Lambda expression: drop parameter to base type

I would like to "wrap" the getter function for a specific property that is part of a specific type. I have an abstract class defined as follows:

public abstract class MyAbstractClass<T> where T : MyType { // ... } 

Ok, suppose I have a specific class, like the following:

 public abstract class MyConcreteClass : MyAbstractClass<MyConcreteType> { // ... } 

And now, a helper method that should return a wrapper for the getter method:

 private Func<MyAbstractClass<T>, Object> GetPropertyGetter(PropertyInfo property) { var instanceType = Expression.Parameter(property.DeclaringType, "i"); // Taking getter "body". var getterBody = Expression.Property(instanceType, property); // Cast to a generic Object. var body = Expression.TypeAs(getterBody, typeof(Object)); // Build the expression. var exp = Expression.Lambda<Func<MyAbstractClass<T>, Object>>(body, instanceType); return exp.Compile(); } 

As expected, I get the following exception:

The Expression of type 'MyConcreteClass' cannot be used to delegate a parameter of type 'MyAbstractClass <MyConcreteType>'.

Is there any way to β€œforce” this kind of casting? Thanks in advance.

+7
source share
2 answers

If I understand your question correctly, you want to create a lambda expression as follows:

 Func<MyAbstractClass<T>, Object> f = i => ((MyConcreteClass)i).SomeProperty; 

Except that you want to specify which property SomeProperty as a parameter. Well, if you want to programmatically create this expression, you need to do the same thing: have an expression parameter i (of type MyAbstractClass<T> ), drop it to MyConcreteClass and then access this SomeProperty property.

 public Func<MyAbstractClass<T>, Object> GetPropertyGetter(PropertyInfo property) { var parameter = Expression.Parameter(typeof(MyAbstractClass<T>), "i"); var cast = Expression.TypeAs(parameter, property.DeclaringType); var getterBody = Expression.Property(cast, property); var exp = Expression.Lambda<Func<MyAbstractClass<T>, Object>>( getterBody, parameter); return exp.Compile(); } 

Having said that, I have no idea why you want to do this, so you better be sure that this is what you want to do and that there is no better way to do what you really want to do.

+12
source

Shouldn't an instance type be created from a generic type?

 var genericType = typeof(MyAbstractClass<>).MakeGenericType(property.DeclaringType); var instanceType = Expression.Parameter(genericType , "i"); 
0
source

All Articles