我試圖找出如何創建一個調用具有基準參數的方法的表達。如何調用參考變量的方法與表達式樹
讓我解釋一下我用一個簡單的(但人造的)例子問題。考慮方法:
public static int TwiceTheInput(int x)
{
return x*2;
}
我可以創造一個表達的做類似調用上述方法:
{
var inputVar = Expression.Variable(typeof (int), "input");
var blockExp =
Expression.Block(
new[] {inputVar}
, Expression.Assign(inputVar, Expression.Constant(10))
, Expression.Assign(inputVar, Expression.Call(GetType().GetMethod("TwiceTheInput", new[] { typeof(int) }), inputVar))
, inputVar
);
var result = Expression.Lambda<Func<int>>(blockExp).Compile()();
}
在執行過程中,「結果」上面應該結束了,20的值。 現在考慮一個版本,使用由基準參數TwiceTheInput()的:
public static void TwiceTheInputByRef(ref int x)
{
x = x * 2;
}
我怎樣寫一個類似的表達式樹調用TwiceTheInputByRef(),並通過參照它的參數?
解決方案:(感謝蟬)。用途:
Type.MakeByRefType()
這裏有一個代碼段生成表達式樹:
{
var inputVar = Expression.Variable(typeof(int), "input");
var blockExp =
Expression.Block(
new[] { inputVar }
, Expression.Assign(inputVar, Expression.Constant(10))
, Expression.Call(GetType().GetMethod("TwiceTheInputByRef", new[] { typeof(int).MakeByRefType() }), inputVar)
, inputVar
);
var result = Expression.Lambda<Func<int>>(blockExp).Compile()();
}
您是否嘗試過使用lambda表達式調用相同的方法,讓C#編譯器將其轉換爲表達式樹,然後進行反編譯?這通常是我做的如何構建表達樹:) – 2013-02-18 15:55:26
不,我沒有做過。任何通過示例演示的網址? – DPrb 2013-02-18 16:02:48