c# - Get generic method definition of implemented generic class -
public class target3<t,t2> { t gettgenerictest(t2 obj); }
in above code, gettgenerictest
not genericmethod, makegenericmethod
can of course not used. so, when have methodinfo
belonging object of iinterfacetarget3<string,int>
, can work it's non-defined version? can't use makegenerictype because have no way link current methodinfo
non-defined one.
(pseudo code keep short)
list<methodinfo> undefs = new list<methodinfo>(); void x() { type t = typeof(target3<,>); methodinfo undefmi = t.getmethod("gettgenerictest"); undefs.add(undefmi); } void y() { var obj = new target3<string,int>(); methodinfo defmi = obj.gettype().getmethod("gettgenerictest"); //how this? methodinfo undefmi = undefs.firstordefault(u=> defmi.undefinedversion() == u); }
if have non-generic method in generic class, not easy "convert" non-generic method in generic class definition. trick:
methodinfo mi1 = typeof(target3<string, int>).getmethod("gettgenerictest"); methodinfo mi2 = mi1.declaringtype.getgenerictypedefinition().getmethod( mi.name, (mi.isstatic ? bindingflags.static : bindingflags.instance) | (mi.ispublic ? bindingflags.public : bindingflags.nonpublic), null, mi.getparameters().select(p => p.parametertype).toarray(), null);
Comments
Post a Comment