Make method static with Roslyn

Before Roslyn days, this was not easy to do that, like others re factoring.

Fortunately we have R# for this.

But now it’s not so hard to re factor an instance method into static method.

I wrote a rewriter that convert any Continue reading

Posted in .NET, c#, Roslyn | Tagged , , , , , | Leave a comment

What is the @ sign before a variable name

You probably saw this at least once

var @delegate = ...

Or especially in generated code (like if you let R# to do some rewriting for you) you can see

var @t = ...

So, what is it? the simple answer is the ‘@’ sign can be added to a reserved word and then you can define for example variable that with the name “@delegate” but you can not define “delegate” variable.

But what special with ‘@’ ? you can add any other characters like “_delegate”?

It’s indeed different because when you add the ‘@’ sign, in the assembly the variable name is Continue reading

Posted in .NET, c# | Tagged , , , , | Leave a comment

Convert linq query syntax to fluent syntax

In c# you can write LINQ queries in two ways. One is the fluent invocation syntax (extension method) and the other is query comprehension syntax.

Fluent example:

Enumerable.Range(1, 5).
Where(i => i % 2 == 0).
Select(i => i * 2);

Comprehension example:

from i in Enumerable.Range(1, 5)
where i % 2 == 0
select i * 2;

I was needed the ability to take the comprehension syntax and convert it to fluent syntax. So I wrote a rewriter with Roslyn that do exactly this.

Continue reading

Posted in .NET, c#, Roslyn | Tagged , , , , , , | Leave a comment

Low-Level Global Keyboard Hook in C#

I had to implement a mechanism for a global low-level keyboard hook.

After a brief search on the internet I understood the principle and I went out with solution in hand.

First I show you how to use it, it’s very simple.

To register a new hook, just write the following:

hookId = GlobalKeyboardHook.Instance.Hook(new List { Key.LeftCtrl, Key.D }, SomeMethod, out message);

And to remove hook registration, write the following:

GlobalKeyboardHook.Instance.UnHook(hookId);

Here is the full code. You can use it as is if you want.

Continue reading

Posted in .NET | Tagged , , , , , , , , | Leave a comment

Localization using ResourceManager

Multiple language support is a very important thing to think of it in early software development process to avoid unnecessary work later on when we realize that we need it.

So how is it done?
There are several ways to support this and here I will describe one option supported in .NET framework using .resx files
In principle, it is possible to insert any text required resx file and then access it in the following way:

Properties.Resources.Cars.Nissan;

So instead of writing hard-coded strings, we will get them from resource. This approach suffers from several drawbacks.

Continue reading

Posted in .NET, c#, WPF | Tagged , , | Leave a comment

IL Call Vs. Callvirt Instruction Part Two

In the first part I wrote about what is call and callvirt, what the differences and why should I care.
We saw that even non-virtual methods called via callvirt instruction because the null check reason.
But with this sense when this is not in use or when this can’t be null there is no need to check for nullfor example: static class. And indeed for those cases the compiler emit call instruction.

Here is the cases that call instruction will emit:
Continue reading

Posted in .NET | Tagged , , , , , , | Leave a comment