tags:

views:

77

answers:

2

Hi,

is there any way in .Net 2.0 to retrieve a property name from delegate?: i => i.Name

When I call: var property = MyMethod(i => i.Name);

I want MyMethod to return string "Name". So the value of 'property' should be "Name".

In .Net 3.5 there is simply way to do that (Expression Tree), but I have to use 2.0 Framework only.

Chris

+3  A: 

No, that's not possible because .NET 2.0 doesn't support expression trees. An anonymous delegate is always compiled, it can't be parsed as an expression

Thomas Levesque
+3  A: 

In .NET 2.0 you'd have to get the target method from the body, call MethodBase.GetMethodBody and then parse the IL. This would not be an easy task, I suspect.

One option would be to use the Mono expression tree in their System.Core implementation against .NET 2.0, but still compile with a C# 3 compiler. I have heard reports that this works fine, but it's a bit of a drastic workaround.

Jon Skeet