运行 基于文件名的正确方法 (C#)
Run correct method based on file name (C#)
我正在检查 file
和 return TRUE
的名称是否正确:
bool name_FORD = file.Contains("FORD");
bool name_KIA = file.Contains("KIA");
bool name_BMW = file.Contains("BMW");
基于此我想切换和运行正确method
。但我不知道如何正确地做到这一点:
switch (true)
{
case 1 name_FORD:
method1();
break();
case 2 name_KIA:
method2();
break();
case 3 name_BMW:
method3();
break();
}
我建议组织所有字符串和相应的方法作为Dictionary
:
Dictionary<string, Action> myCars = new Dictionary<string, Action>() {
{"FORD", method1}, // e.g. {"FORD", () => {Console.WriteLine("It's Ford!");}},
{ "KIA", method2},
{ "BMW", method3},
//TODO: Put all the cars here
};
然后我们可以放一个简单的循环:
foreach (var pair in myCars)
if (file.Contains(pair.Key)) { // if file contains pair.Key
pair.Value(); // we execute corresponding method pair.Value
break;
}
编辑: 如果我们可以有复杂的方法(例如方法可能需要 file
和 key
参数)我们可以更改签名:
// Each action can have 2 parameters: key (e.g. "FORD") and file
Dictionary<string, Action<string, string>> myCars =
new Dictionary<string, Action<string, string>>() {
{"FORD", (key, file) => {Console.Write($"{key} : {string.Concat(file.Take(100))}")}},
{ "KIA", (key, file) => {Console.Write($"It's {key}!")}},
{ "BMW", (key, file) => {/* Do nothing */}},
//TODO: Put all the cars here
};
在循环中执行时,我们应该提供这些参数:
foreach (var pair in myCars)
if (file.Contains(pair.Key)) { // if file contains pair.Key
pair.Value(pair.Key, file); // we execute corresponding method pair.Value
break;
}
您可以通过将它们分配给 Action 来使用方法,例如 c# 中的变量:
public void KiaMethod(){
Console.WriteLine("Kia");
}
public void BmwMethod(){
Console.WriteLine("BMW");
}
Action method = null;
if(file.Contains("KIA"))
method = KiaMethod;
else if(file.Contains("BMW"))
method = BmwMethod;
method();
虽然我真的很喜欢 Keiran 的回答中的模式,因为我真的不明白为什么你需要这种复杂程度
我正在检查 file
和 return TRUE
的名称是否正确:
bool name_FORD = file.Contains("FORD");
bool name_KIA = file.Contains("KIA");
bool name_BMW = file.Contains("BMW");
基于此我想切换和运行正确method
。但我不知道如何正确地做到这一点:
switch (true)
{
case 1 name_FORD:
method1();
break();
case 2 name_KIA:
method2();
break();
case 3 name_BMW:
method3();
break();
}
我建议组织所有字符串和相应的方法作为Dictionary
:
Dictionary<string, Action> myCars = new Dictionary<string, Action>() {
{"FORD", method1}, // e.g. {"FORD", () => {Console.WriteLine("It's Ford!");}},
{ "KIA", method2},
{ "BMW", method3},
//TODO: Put all the cars here
};
然后我们可以放一个简单的循环:
foreach (var pair in myCars)
if (file.Contains(pair.Key)) { // if file contains pair.Key
pair.Value(); // we execute corresponding method pair.Value
break;
}
编辑: 如果我们可以有复杂的方法(例如方法可能需要 file
和 key
参数)我们可以更改签名:
// Each action can have 2 parameters: key (e.g. "FORD") and file
Dictionary<string, Action<string, string>> myCars =
new Dictionary<string, Action<string, string>>() {
{"FORD", (key, file) => {Console.Write($"{key} : {string.Concat(file.Take(100))}")}},
{ "KIA", (key, file) => {Console.Write($"It's {key}!")}},
{ "BMW", (key, file) => {/* Do nothing */}},
//TODO: Put all the cars here
};
在循环中执行时,我们应该提供这些参数:
foreach (var pair in myCars)
if (file.Contains(pair.Key)) { // if file contains pair.Key
pair.Value(pair.Key, file); // we execute corresponding method pair.Value
break;
}
您可以通过将它们分配给 Action 来使用方法,例如 c# 中的变量:
public void KiaMethod(){
Console.WriteLine("Kia");
}
public void BmwMethod(){
Console.WriteLine("BMW");
}
Action method = null;
if(file.Contains("KIA"))
method = KiaMethod;
else if(file.Contains("BMW"))
method = BmwMethod;
method();
虽然我真的很喜欢 Keiran 的回答中的模式,因为我真的不明白为什么你需要这种复杂程度