2013-04-03 15 views
1

我:調用Expression.Call

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; 
     } 
    } 
} 

我得到Y`1跳過(Int32)已包含泛型參數。

不能使該方法通用的,所以我可以調用method.MakeGenericType

如何我可以創建Expression.Call任何想法?

我也試過:

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

這個時候,我得到:

無方法 '跳過' 存在於型 'X 1+Y 1 [T1,T2]'。

+1

你可以把這個成爲一個工作的例子嗎? –

回答

1

這似乎工作:

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

順便說一句,Type.EmptyTypes相當於new Type[0]

+0

謝謝,它的工作原理! – Andrei

+0

太好了,別忘了接受這個答案。 ;-) – luksan

0

the documentation所述,GetCurrentMethod不會填充擁有當前方法的類型的通用參數。

相反,一個選項是使用:

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

此外,第一個參數不應該null,但這是另外一個問題。

相關問題