有没有可能用更短、可读的代码编写下一个开关?
switch (SomeValue)
{
case "001": return DoMethod1(); break;
case "002": return DoMethod2(); break;
//etc..
}
Run Code Online (Sandbox Code Playgroud)
我在想以某种方式
Dictionary<string, Func<int>> MethodsByValue = new Dictionary<string, Func<int>>()
{
{ "001", DoMethod1 },
{ "002", DoMethod2 },
}
Run Code Online (Sandbox Code Playgroud)
并通过这样做来调用它
return MethodsByValue[SomeValue]();
Run Code Online (Sandbox Code Playgroud)
但这可能吗?或者说我的想法太离谱了。我找不到这样的东西,但话又说回来,如果可能的话,我不知道这个的关键字。
编辑:回答 Lasse V. Karlsen 的请求:
这就是我的项目中的代码的样子。在某些地方更改了名称,因为原来的名称并不重要,因为它是我的母语。
public string GetRecord420(Dictionary<DataClass, object> dictionaryName)
{
// some code here
}
public string GetRecord421(Dictionary<DataClass, object> dictionaryName)
{
// some code here
}
//(Temperary) solution with the switch statement in a wrapper:
public string GetRecordByString(string s, Dictionary<DataClass, object> dictionaryName)
{
switch (s)
{
case "320": return GetRecord420(dictionaryName);
case "321": return GetRecord421(dictionaryName);
default: return String.Empty;
}
}
//How I hoped it could be, with a mapping dictionary.
public Dictionary<string, Func<string, Dictionary<DataClass, object>>> MethodByString =
new Dictionary<string, Func<string, Dictionary<DataClass, object>>>()
{
{ "320", GetRecord420 },
{ "321", GetRecord421 },
}
Run Code Online (Sandbox Code Playgroud)
DataClass是一个Entity类,它存储一些列数据(列名、列类型等)。
我尝试了字典部分,但它给了我错误:无法从方法组转换为 System.Func<...>。
更改为 () => GetRecord420 会出现错误:无法将 lambda 转换为委托类型 System.Func<...> 因为块中的某些返回类型不能隐式转换为委托返回类型。
您的方法定义一定有错误,
class Program
{
static void Main()
{
var methods = new Dictionary<string, Func<int>>
{
{ "001", DoMethod1 }
};
}
static int DoMethod1()
{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
是完全有效的语法。
但是,这并不比switch1 个令人信服的和 1 个主观原因更好。
如果您要与常量或文字进行比较,那么您应该使用 switch。这使得编译器无需额外分析即可执行编译时优化。
更主观地说,字典/查找方法并不短,而且我发现它更难阅读。但是,在比较项在运行时发生变化的情况下,它会很有用。
如果您想避免将switch因子重写为函数。说,
Func<int> MethodsByValue(string value)
{
switch(value)
{
case "001":
return DoMethod1;
default:
return DoMethod2;
}
}
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,
与其使用一些任意字符串来枚举您的方法,为什么不使用enum?然后您将获得额外的性能和可读性优势。
| 归档时间: |
|
| 查看次数: |
2893 次 |
| 最近记录: |