I have a class that inherits a generic base class.
public class MyClass: BaseWithStatic<MyClass> { }
In a generic extension method for said class, I'm trying to call a static method that should be on T
, though all the call returns is null when it works all.
public static <T> ToThingForAllT (this object val)
where T: BaseWithStatic<T> {
T tmp;
// Not available
T.TheStaticMethod(val, out tmp);
// Fails silently, likely because not a actual T
BaseWithStatic<MyClass>.TheStaticMethod(val);
// If you use BaseWithStatic<T> it produces an error "Ambiguous match..." on invoke
var tType = typeOf(T);
var staticMethod = tType.GetMethod("TheStaticMethod");
// pass null as first param because static method, value, outParam
var T tmp;
var result = staticMethod.Invoke(null, new[] { val, tmp });
}
What is the correct way to access static methods on a class through reflection? Does the fact that it's a generic change that process at all?
Aucun commentaire:
Enregistrer un commentaire