小编use*_*864的帖子

如何使用反射调用通用异步方法

public interface IBar {
}
public class Bar : IBar {
}
public class Bar2 : IBar {
}
public interface IFoo {
  Task<T> Get<T>(T o) where T : IBar;
}
public class Foo : IFoo {
  public async Task<T> Get<T>(T o) where T : IBar {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我可以使用反射调用此方法:

var method = typeof(IFoo).GetMethod(nameof(IFoo.Get));
var generic = method.MakeGenericMethod(bar2.GetType());
var task = generic.Invoke(foo, new [] { bar2 });
Run Code Online (Sandbox Code Playgroud)

我该如何等待Task?以及如何将其投射到Task<bar2.GetType()>

c# reflection async-await

20
推荐指数
3
解决办法
9652
查看次数

标签 统计

async-await ×1

c# ×1

reflection ×1