我有这个方法,
public void SomeMethod() { ... }
我可以使用反射毫无问题地创建委托,
var action = (Action)method.CreateDelegate(typeof(Action), this);
...
action.Invoke();
但是,如果我将void
替换为async Task
返回类型,则会出现此错误,
Cannot bind to the target method because its signature is not compatible with that of the delegate type.
如果方法如下,我找不到如何创建委托并调用它的方法,
public async Task SomeMethod() { ... }
或者像这样,
public async Task<SomeObject> SomeMethod() { ... }
感谢您的帮助。
如果我正确理解你的问题
鉴于
public async Task SomeBob() { ... }
我猜你正在寻找
MethodInfo method = //<Some wonderful reflection here>
var bob = (Func<Task>)method.CreateDelegate(typeof(Func<Task>), this);
await bob();
或
(Func<Task<SomeType>>)method.CreateDelegate(typeof(Func<Task<SomeType>>), this);
虽然至于你这样做或需要这样做的方式,我不确定