c# - Reflection: How to get a generic method? -
possible duplicates:
how use reflection call generic method?
select right generic method reflection
hi there
let's have 2 following 2 methods in class:
public void mymethod(object val) {} public void mymethod<t>(t val) {}
with reflection, first method this:
type[] typearray = new type[1]; typearray.setvalue(typeof(object), 1); var mymethod = myinstance.gettype().getmethod("mymethod", typearray);
but how can second, generic method?
var mymethod = myinstance.gettype() .getmethods() .where(m => m.name == "mymethod") .select(m => new { method = m, params = m.getparameters(), args = m.getgenericarguments() }) .where(x => x.params.length == 1 && x.args.length == 1 && x.params[0].parametertype == x.args[0]) .select(x => x.method) .first();
Comments
Post a Comment