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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -