Await the result of Task using reflection in a non-generic method

后端 未结 2 1876
不知归路
不知归路 2021-01-12 01:03

Consider the following case:

class A
{
    public int Id;
}

class B : A
{

}

class Main
{
    public async Task Create(Type type)
    {
                 


        
2条回答
  •  囚心锁ツ
    2021-01-12 01:52

    The above solution really helped me. I made a small tweak to the @Lukazoid solution...

    var resultProperty = typeof(Task<>).MakeGenericType(type).GetProperty("Result");
    A a = (A)resultProperty.GetValue(task);
    

    to

    dynamic a = task.GetType().GetProperty("Result")?.GetValue(task);
    

提交回复
热议问题