我想根据指定方法名称的文本文件在运行时将委托与方法相关联。使用像
这样的东西会更快吗?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。)