dimanche 20 août 2017

How can I pass the unknown type of an instance to a generic method?

I have a method with the signature:

public static void MyMethod<T>()

I want to call it passing the generic type of some arbitrary variable:

var myFoo = new Foo(); //I do not know that this is Foo
var genericType = ???; //What should I write here?
MyMethod<genericType>();

How can I pass the generics of an instance in a method?


What have I tried? A bunch of things. An example, of one that does not compile:

var genericType = myFoo.GetType();





Aucun commentaire:

Enregistrer un commentaire