爲什麼不能正常工作?如何使用Compile在表達式<Action<T>>上調用DynamicInvoke?
namespace InvokeTest
{
public class MethodInvoker
{
public static void Execute<T>(Expression<Action<T>> call)
{
// Parameter count mismatch
call.Compile().DynamicInvoke();
// Also attempted this:
//call.Compile().DynamicInvoke(1);
// but it threw: "Object of type 'System.Int32' cannot be converted to type 'InvokeTest.TestClass'."
}
}
public class TestClass
{
public TestClass()
{ }
public void DoSomething(int num)
{
System.Console.WriteLine("It works");
}
public void KickOff()
{
MethodInvoker.Execute<TestClass>(m => m.DoSomething(1));
}
}
}