是否更好地使用Type.GetMethod和Delegate.CreateDelegate或使用switch手动关联委托与mothod? C#

时间:2013-06-11 09:45:31

标签: c# performance reflection delegates

我想根据指定方法名称的文本文件在运行时将委托与方法相关联。使用像

这样的东西会更快吗?
using System;
using System.Reflection;

class MethodCollection
{
    public static void Method1(){};
    public static void Method2(){};
}


delegate void DelegateDef();

void ExecuteMethod(string methodName)
{
    DelegateDef myDelegate;
    Type type=typeof(MethodCollection)

    MethodInfo methodInfo=type.GetMethod(methodName);
    myDelegate=Delegate.CreateDelegate(typeof(DelegateDef),methodInfo);
    myDelegate();
}

void ExecuteMethod(string methodName)
{
    DelegateDef myDelegate;
    Type type=typeof(MethodCollection)

    if (methodName=="Method1")
    {
        myDelegate+=MethodCollection.Method1;
    }
    else if (methodName=="Method2")
    {
        myDelegate+=MethodCollection.Method2;
    }

    myDelegate();
}

(我认为这是无关紧要的,但我的目标是iOS和Android。)

0 个答案:

没有答案