Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Note: This blog has moved to omervk.wordpress.com.

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Expression trees' ConstantExpression values

After reading Jafar Husain's posts (1, 2 and bonus by Doron Yaacoby), I started thinking about being able to strong type names using the Expression classes.

One of the things I came up with is creating a tracing framework where Expression is passed, instead of strings or other untyped methods.

[Conditional("DEBUG")]
public static void TraceStart<T, R>(
this T me, Expression<Func<T, R>> where) {
// ...
}

[Conditional("DEBUG")]
public static void TraceEnd<T, R>(
this T me, Expression<Func<T, R>> where, R returnValue) {
// ...
}

class Foo
{
public bool Bar(int param)
{
this.TraceStart(o => o.Bar(param));

this.TraceEnd(o => o.Bar(param), false);

return false;
}
}

Since the TraceStart and TraceEnd methods are decorated with a ConditionalAttribute, the Expression objects will not even be created (since the calls will not be made).

Nice idea, but the overhead during debugging might just be too much.

 

Here's where the interesting bit comes in: While I was fiddling with the idea, I tried to print the value of param, by reaching the expression tree's ConstantExpression for it. However, the type in the expression's Value property was of a type named: Namespace.Foo+<>c__DisplayClass0.

OK, so where do I get the value from? Apparently, Namespace.Foo+<>c__DisplayClass0 has a public field named value. That's where. Oh, but wait. You can't get it - since it's a field, you can't get it using polymorphism, but even if it was a property, Namespace.Foo+<>c__DisplayClass0 doesn't inherit or implement anything.

Resorting to reflection seems like a terrible solution. I just hope that this will change until C# 3.0 RTMs.

Posted: Oct 03 2007, 08:25 PM by Omer van Kloeten | with 4 comment(s)
Filed under: ,

Comments

No Comments