Andrei
Andrei

Reputation: 13

calling Expression.Call

I have:

class X<t1>
{
   class Y<t2>
   {

      public Y<t2> Skip(int count)
      {
          var mi = (MethodInfo)MethodBase.GetCurrentMethod();
          var f = Expression.Call(null, mi,Expression.Constant(count));
          var x = this.Provider.CreateQuery(f);
          return something_else;
      }
   }
}

I get Y`1 Skip(Int32) contains generic parameters.

Can't make the method generic so i can call method.MakeGenericType

Any idea on how i can create the Expression.Call ?

I also tried :

var f = Expression.Call(typeof(Y<>), "Skip", new Type[] { gt }, Expression.Constant(count));

this time i get:

No method 'Skip' exists on type 'X1+Y1[t1,t2]'.

Upvotes: 1

Views: 1223

Answers (2)

luksan
luksan

Reputation: 7757

This seems to work:

var f = Expression.Call(
     Expression.Constant(this), 
     "Skip", 
     Type.EmptyTypes, 
     Expression.Constant(count));

By the way,Type.EmptyTypes is equivalent to new Type[0].

Upvotes: 1

Jacob
Jacob

Reputation: 1739

As noted by the documentation, GetCurrentMethod does not fill in the generic arguments of the type(s) that own the current method.

Instead, an option is to use:

MethodInfo mi = typeof(X<t1>.Y<t2>).GetMethod("Skip");
Expression f = Expression.Call(null, mi, Expression.Constant(count))

Also, the first parameter shouldn't be null, but that's a separate issue.

Upvotes: 0

Related Questions