2
運行Run2方法。但Run方法不運行。是什麼原因 ? 兩種方法之間的唯一區別是因爲參數。爲什麼我使用Delegate.CreateDelegate獲得這個錯誤'綁定到目標方法'?
public class MyClass
{
public string Name { get; set; }
}
[TestFixture]
public class Test
{
public IEnumerable<T> TestMethod<T>(int i)
{
//do something
return null;
}
public IEnumerable<T> TestMethod2<T>()
{
//do something
return null;
}
[Test]
public void Run()
{
MethodInfo mi = this.GetType().GetMethod("TestMethod").MakeGenericMethod(typeof(MyClass));
var del = Delegate.CreateDelegate(typeof(Func<IEnumerable<MyClass>>), this, mi);
var list = (IEnumerable<MyClass>)del.DynamicInvoke(0);
}
[Test]
public void Run2()
{
MethodInfo mi = this.GetType().GetMethod("TestMethod2").MakeGenericMethod(typeof(MyClass));
var del = Delegate.CreateDelegate(typeof(Func<IEnumerable<MyClass>>), this, mi);
var list = (IEnumerable<MyClass>)del.DynamicInvoke();
}
}