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.