0
我正在嘗試使用表達式樹創建的靜態方法調用的字符串表示形式。但是,文本表示不包含方法調用的FQN。下面給出的代碼輸出TestMethod()而不是AnotherClass.TestMethod()我需要的。在Expression.Call中顯示FQN
編輯:這只是一個簡單的例子。最終輸出可能是這樣的: -
AnotherClass.TestMethod<Guid>("BLOB_DATA", new MyClass())
所以,我不是試圖只是獲得方法的FQN。根表達式對象可能不是一個方法調用。我認爲不管表達式有多複雜,做一個ToString()都會返回可以表示它的C#代碼。
目標是將根表達式轉換爲我可以在內存中使用和編譯的C#代碼片段。
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace ExpressionTest
{
internal class Program
{
private static void Main(string[] args)
{
// Variant 1
MethodCallExpression call = Expression.Call(typeof (AnotherClass), "TestMethod", Type.EmptyTypes);
Console.WriteLine(call.ToString());
// Variant 2
MethodInfo method = typeof (AnotherClass).GetMethod("TestMethod");
MethodCallExpression call2 = Expression.Call(method);
Console.WriteLine(call2.ToString());
Console.ReadLine();
}
}
internal class AnotherClass
{
public static void TestMethod()
{
}
}
}
我不是試圖獲得一個方法的FQN。目標是將根表達式轉換爲我可以在內存中使用和編譯的C#代碼。抱歉不清楚,請查看我對這個問題的編輯。 – Partho
如果您想編譯表達式以便能夠調用它,那麼您肯定不希望通過表達式上的ToString()調用。 Expression.Lambda()和Lambda .Compile()是你的朋友在這裏 –