Ho scritto il metodo di estensione GenericExtension
. Ora voglio chiamare il metodo di estensione Extension
. Ma il valore di methodInfo
è sempre nullo.Come chiamare un metodo di estensione generico con riflessione?
public static class MyClass
{
public static void GenericExtension<T>(this Form a, string b) where T : Form
{
// code...
}
public static void Extension(this Form a, string b, Type c)
{
MethodInfo methodInfo = typeof(Form).GetMethod("GenericExtension", new[] { typeof(string) });
MethodInfo methodInfoGeneric = methodInfo.MakeGenericMethod(new[] { c });
methodInfoGeneric.Invoke(a, new object[] { a, b });
}
private static void Main(string[] args)
{
new Form().Extension("", typeof (int));
}
}
Cosa c'è di sbagliato?
Vedi http://stackoverflow.com/questions/5959219/how -to-use-reflection-to-get-extension-method-on-generic-type? rq = 1 per maggiori informazioni su questo. – skarmats