我正在处理一些现有的代码。几个小时后,我把这个问题归结为一个方法选择器类。我觉得这门课很难学。是否有可能以简单的方式实现这种类型的方法选择功能?
public class MethodPicker
{
private delegate string SomeFunc(MethodPicker item);
static readonly Dictionary<string, SomeFunc> test = new Dictionary<string, SomeFunc>();
static MethodPicker()
{
test.Add("key1", Func1);
test.Add("key2", Func2);
test.Add("key3", Func3);
}
public string RunTest(string Name)
{
string somestring = test[Name].Invoke(this);
return somestring;
}
public static string Func1(MethodPicker entity)
{
return "func1 runs";
}
public static string Func2(MethodPicker entity)
{
return "func2 runs";
}
public static string Func3(MethodPicker entity)
{
return "func3 runs";
}
}