我想知道是否有一个更有效(在更简单/更简洁的代码中有效)的方式来制作如下所示的案例陈述......
我有一本字典.它的关键类型是Enum,它的值类型是bool.如果布尔值为true,我想更改表单上标签的颜色.
为示例更改了变量名称.
Dictionary<String, CustomType> testDict = new Dictionary<String, CustomType>();
//populate testDict here...
Dictionary<MyEnum, bool> enumInfo = testDict[someString].GetEnumInfo();
//GetEnumInfo is a function that iterates through a Dictionary<String, CustomType>
//and returns a Dictionary<MyEnum, bool>
foreach (KeyValuePair<MyEnum, bool> kvp in enumInfo)
{
switch (kvp.Key)
{
case MyEnum.Enum1:
if (someDictionary[kvp.Key] == true)
{
Label1.ForeColor = Color.LimeGreen;
}
else
{
Label1.ForeColor = Color.Red;
}
break;
case MyEnum.Enum2:
if (someDictionary[kvp.Key] == true)
{
Label2.ForeColor = Color.LimeGreen;
}
else
{
Label2.ForeColor = Color.Red;
}
break;
} …Run Code Online (Sandbox Code Playgroud)