I am trying to get the string representation of a static method call that I created using expression trees. However, the textual representation does not contain the FQN of the method call. The code given below outputs TestMethod() instead of AnotherClass.TestMethod() which I need.
Edit: This is just a simple example. Ultimately the output can be something like this:-
AnotherClass.TestMethod<Guid>("BLOB_DATA", new MyClass())
So, I am not trying to just obtain the FQN of a method. The root expression object might not even be a method call. I thought that no matter how complex the expression is, doing a ToString() will return the C# code that can represent it.
The goal is to convert the root expression into C# code snippet that I can use and compile in memory.
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()
{
}
}
}
string.Format("{0}.{1}", method.DeclaringType.Name, method.Name)
If (per recent edit to your Q) you want to turn the expression into something executable, you can instead do something like:
Action compiled = Expression.Lambda<Action>(call2).Compile();
You can then call the compiled expression as you would any other Action