.NET Reflector Give Away

Red Gate gave me a few licenses of .NET Reflector to give away.

The first to answer to this post in any of my blogs with a phrase about .NET Reflector and name and contact e-mail will get a free license.

Hurry up! There aren’t many!

Update: I've given all licenses. Sorry for those who didn't get one.

.NET Reflector 7 Released

This new version fixes a number of bugs and adds support for more high level C# features such as iterator blocks.

A new tabbed browsing model was added and Jason Haley's PowerCommands add-in was included as an exploratory step for future versions.

To find out more about version 7 just visit

The release of version 7 also means that the free version of .NET Reflector is no longer available for download. Maybe you can still get one of the give away licenses that Red Gate provided to communities and individuals.

Hydrating Objects With Expression Trees – Part I

LINQ With C# (Portuguese)

After my post about dumping objects using expression trees, I’ve been asked if the same could be done for hydrating objects.

Sure it can, but it might not be that easy.

What we are looking for is a way to set properties on objects of an unknown type. For that, we need to generate methods to set each property of the objects.

Such methods would look like this expression:

Expression<Action<object, object>> expression = (o, v) => ((SomeType)o).Property1 = (PropertyType)v;

Unfortunately, we cannot use the .NET Reflector trick because, if you try to compile this, you’ll get this error:

error CS0832: An expression tree may not contain an assignment operator

Fortunately, that corresponds to a valid .NET expression tree. We just have to build it by hand.

So, for a given type, the set of property setters would be built this way:

var compiledExpressions = (from property in objectType.GetProperties()
                           let objectParameterExpression = Expression.Parameter(typeof(object), "o")
                           let convertedObjectParameteExpressionr = Expression.ConvertChecked(objectParameter, objectType)
                           let valueParameter = Expression.Parameter(propertyType, "v")
                           let convertedValueParameter = Expression.ConvertChecked(valueParameter, property.PropertyType)
                           let propertyExpression = Expression.Property(convertedObjectParameter, property)
                                Expression.Lambda<Action<object, object>>(

And hydrating objects would be like this:

for (int o = 0; o < objects.Length; o++)
    var objectProperties = objects[o];

    var newObject = newObjects[o] = Activator.CreateInstance(objectType);

    for (int p = 0; p < compiledExpressions.Length; p++)
        compiledExpressions[p](newObject, objectProperties[p]);

Mastering Expression Trees With .NET Reflector

Following my last post, I received lots of enquiries about how got to master the creation of expression trees.

The answer is: .NET Reflector

On that post I needed to to generate an expression tree for this expression:

Expression<Func<object, object>> expression = o => ((object)((SomeType)o).Property1);

I just compiled that code in Visual Studio 2010, loaded the assembly in .NET Reflector, and disassembled it to C# without optimizations (View –> Options –> Disassembler –> Optimization: None).

The disassembled code looked like this:

Expression<Func<object, object>> expression;
ParameterExpression CS$0$0000;
ParameterExpression[] CS$0$0001;
expression = Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Property(Expression.Convert(CS$0$0000 = Expression.Parameter(typeof(object), "o"), typeof(SomeType)), (MethodInfo) methodof(SomeType.get_Property1)), typeof(object)), new ParameterExpression[] { CS$0$0000 });

After giving valid C# names to the variables and tidy up the code a bit, I came up with this:

ParameterExpression parameter = Expression.Parameter(typeof(object), "o");
Expression<Func<object, object>> expression =
    Expression.Lambda<Func<object, object>>(

Easy! Isn’t it?

PowerShell For The .NET Developer

Some time ago I needed to have the validationKey of the machineKey element of an ASP.NET application changed and found out that ASP.NET doesn’t provide a command-line tool (or any other) to do this.

Looking around I found several applications and code samples to do it, but to have a system administrator do this I needed to test and document the application and it was to much work for such task.

I’ve always been a supporter of the idea of PowerShell but I never used it my self. Just because I almost always have Visual Studio open and writing a simple console application is quicker and easier than learning PowerShell.

This time I decide that I would do a PowerShell script instead.

In C# I would have done something like this:

class Program
    private static string GenerateKey()
        var buff = new byte[64];
        (new System.Security.Cryptography.RNGCryptoServiceProvider()).GetBytes(buff);
        var sb = new System.Text.StringBuilder();
        foreach (var b in buff)
            sb.AppendFormat("{0:X2}", b);
        return sb.ToString();

    private static void Main(string[] args)
        var path = args[0];
        var config = System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration(path);
        var systemWeb = config.GetSectionGroup("system.web") as System.Web.Configuration.SystemWebSectionGroup;
        var machineKey = systemWeb.MachineKey;
        machineKey.ValidationKey = GenerateKey();

How would it be in PowerShell? As simple as this:

function GenerateKey
    [System.Byte[]]$buff = 0..63
    (new-object System.Security.Cryptography.RNGCryptoServiceProvider).GetBytes($buff)
    $sb = new-object System.Text.StringBuilder(128)
    for($i = 0; ($i -lt $buff.Length); $i++)
        $sb = $sb.AppendFormat("{0:X2}", $buff[$i])
    return $sb.ToString()

$config = [System.Web.Configuration.WebConfigurationManager]::OpenWebConfiguration("<path>")
$systemWeb = $config.GetSectionGroup("system.web");
$machineKey = $systemWeb.MachineKey

Wonder how I got from no knowledge of PowerShell to this? Simple. Something that every real .NET developer has and loves: .NET Reflector (with a PowerShell add-in, of course).

redgate .NET Reflector Released

Today redgate released version of .NET Reflector.

It was a bumpy release. Early today, at work, as soon as @antsprofiler was announced the deployment of the new version I tried to upgrade and was offered an upgrade to the version over and over again.

Early tonight, at home, when I tried to upgrade I was offered to upgrade to

The funny thing is that, if I try to run it from C:\Program Files I get a UAC dialog the mentions Reflector 5.1.3.

Just moved to my C:\Program Files (Utils) and it all works fine.

Lutz Roeder’s .NET Reflector Is Now Red Gate’s .NET Reflector

Let’s face it, if you don’t know .NET Reflector, you can never claim to be a .NET developer.

Today Red Gate announced the acquisition of Lutz Roeder’s .NET Reflector.

On .NET Reflector’s page, Red Gate states that “will continue to maintain a free version for the benefit of the community”.

You can read an interview with Lutz Roeder and James Moore (general manager of .NET Developer Tools at Red Gate) at simple-talk.

James doesn’t know yet how to improve Reflector, but I do. Reflector needs a major improvement on UI usability and performance. Let’s see if I can come up with a list:

  • For me, search as you type is not a good idea as it is in Reflector.
  • Still in the search theme, search as you type would be nice for the active code window.
  • I cannot understand why changing any of the options fires a total repaint and lost of the view of the active code item.
  • Settings like code, documentation and number formatting should possible to change on the fly with a simple toolbar click.

I’m sure Red Gate is more than capable of taking good care of .NET Reflector.

.NET Reflector Released

Get it from here.