samedi 24 septembre 2016

How to call a generic async method using reflection

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 {
  public async Task<T> Get<T>(T o) where T : IBar {
    ...
  }
}

I can then call this method using reflection:

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

How do I await on this task? and How do I cast it to Task?





Aucun commentaire:

Enregistrer un commentaire