Paulo Morgado

.NET Development & Architecture

Recent Articles

view all

Events

Projects

Recent Readers

Visitor Locations

Visitor Locations

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

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>>(
        Expression.Convert(
            Expression.Property(
                Expression.Convert(
                    parameter,
                    typeof(SomeType)
                ),
                "Property1"
            ),
            typeof(object)
        ),
        parameter
    );

Easy! Isn’t it?

Comments

The Morning Brew - Chris Alcock » The Morning Brew #659 said:

Pingback from  The Morning Brew - Chris Alcock  &raquo; The Morning Brew #659

# August 6, 2010 2:58 AM

Twitter Trackbacks for Mastering Expression Trees With .NET Reflector - Paulo Morgado [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Mastering Expression Trees With .NET Reflector - Paulo Morgado         [asp.net]        on Topsy.com

# August 6, 2010 8:57 PM

Yasir said:

Why are we getting variables named CS$0$0000? Is there any way to disassemble it correctly i.e. without getting such variable names?

# August 10, 2010 11:00 PM

Paulo Morgado said:

No, Yasir.

These are, actually, these variables are generated by the compiler. The C# compiler generates .NET valid but C# invalid names thus avoiding collisions with any identifier the developer might use.

You can always Find and Replace it using regular expressions. Find CS\${:z}\${:z} (check Match whole word) and replace with CS_\1_\2.

# August 11, 2010 4:32 AM

Paulo Morgado said:

Hydrating Objects With Expression Trees - Part I

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

# August 17, 2010 4:18 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)