lundi 21 septembre 2015

C# - Calling static delegate with reflection

In C#, I need to call a delegate method using reflection, passing a string that adress that method.

For example, I'll pass My.Controls.TestDelegate.myConverterAction where TestDelegate is the class name and myConverterAction is the 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 to use the GetMethod() method in this 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.Instance); //This returns null...even with different BindingFlags options

but I obtain a null reference. How can I solve this problem?





Aucun commentaire:

Enregistrer un commentaire