lundi 6 juin 2016

C# Specify method via string?

I can do something like this to get a TYPE from a string:

Type.GetType("System.Int32, mscorlib")

Now let's say I have this:

class Program
{
    static int Test(int x)
    {
        return x;
    }
    .
    .

I want to specify the Test method in app.config as a string. Is there a standard for this?

I.e. ... converter="ConsoleApplication2.Program.Test, ConsoleApplication2" />

Is there a .Net method already to pull a MethodInfo or preferably the Delegate? Would it work in "all cases" if I get remove the last . section of the part before the comma and get the type on that, then GetMethod for the last part, i.e:

Type type = Type.GetType("ConsoleApplication2.Program");
type.GetMethod("Test")

Or are there any gotchas if I do it like that?





Aucun commentaire:

Enregistrer un commentaire