![](/img/trans.png)
[英]ArgumentException when calling Expression.GreaterThanOrEqual via Expression.Call
[英]calling 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 Skip(Int32)包含通用參數。
無法使方法通用,所以我可以調用method.MakeGenericType
關於如何創建Expression.Call的任何想法嗎?
我也嘗試過:
var f = Expression.Call(typeof(Y<>), "Skip", new Type[] { gt }, Expression.Constant(count));
這次我得到:
類型'X 1+Y
1 [t1,t2]'不存在方法'Skip'。
這似乎可行:
var f = Expression.Call(
Expression.Constant(this),
"Skip",
Type.EmptyTypes,
Expression.Constant(count));
順便說一句, Type.EmptyTypes
等效於new Type[0]
。
如文檔所述 ,GetCurrentMethod不會填充擁有當前方法的類型的泛型參數。
相反,一種選擇是使用:
MethodInfo mi = typeof(X<t1>.Y<t2>).GetMethod("Skip");
Expression f = Expression.Call(null, mi, Expression.Constant(count))
另外,第一個參數不應為null
,但這是一個單獨的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.