title: Lambda Expression: cast parameter to base type
body: <p>I'd like to "wrap" the getter function for a specific property that is part of a specific type.
  I have an abstract class, defined like following:</p>
  
  <pre><code>public abstract class MyAbstractClass&lt;T&gt; where T : MyType
  {
      // ...
  }
  </code></pre>
  
  <p>Well, suppose I've a concrete class like the following one:</p>
  
  <pre><code>public abstract class MyConcreteClass : MyAbstractClass&lt;MyConcreteType&gt;
  {
      // ...
  }
  </code></pre>
  
  <p>And now, the helper method that should return a wrapper for the getter method:</p>
  
  <pre><code>private Func&lt;MyAbstractClass&lt;T&gt;, Object&gt; GetPropertyGetter(PropertyInfo property)
  {
      var instanceType = Expression.Parameter(property.DeclaringType, "i");
  
      // Taking getter's "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&lt;Func&lt;MyAbstractClass&lt;T&gt;, Object&gt;&gt;(body, instanceType);
      return exp.Compile();
  }
  </code></pre>
  
  <p>Just like expected, I get the following exception:  </p>
  
  <blockquote>
    <p>ParameterExpression of type 'MyConcreteClass' cannot be used for
    delegate parameter of type 'MyAbstractClass&lt;MyConcreteType&gt;'.</p>
  </blockquote>
  
  <p>Is there a way to "force" this kind of casting? Thanks in advance.</p>