c# - Calling static delegate with reflection -
i need call delegate method using reflection, passing name of method string.
for example, i'll pass my.controls.testdelegate.myconverteraction
testdelegate
class name , myconverteraction
delegate:
namespace my.controls { public static class testdelegate { public static customconversionhandler myconverteraction = new customconversionhandler(dosomething); private static ulong dosomething(object[] values) { return 2; } } }
i thought use getmethod()
method in way:
int separator = actiondelegate.lastindexof('.'); string classname = actiondelegate.substring(0, separator); string methodname = actiondelegate.substring(separator + 1, actiondelegate.length - classname.length - 1); var t = type.gettype(classname); //this works methodinfo m = t.getmethod(methodname, bindingflags.public | bindingflags.static); //this returns null...even different bindingflags options
but obtain null reference. how can solve problem?
since testdelegate.myconverteraction
static field , not method should retrieve using following code:
fieldinfo info = typeof(testdelegate).getfield("myconverteraction", bindingflags.public | bindingflags.static); object yourfield = info.getvalue(null);
than in order invoke need it's invoke method using:
methodinfo method = yourfield.gettype().getmethod("invoke");
and invoke invoke
method using methodinfo.invoke()
:
method.invoke(yourfield, new {new object[]{"123"}});
Comments
Post a Comment