有没有一种方法可以根据返回的值动态调用相关的Dictionary <string,string>?
因此,在下面的示例中,从TestDictionary返回的值shouldBeDictionary1将基于传递的键G从Dictionary1返回值Green:
private static readonly Dictionary<string, List<string>> TestDictionary = new Dictionary<string, List<string>>
{
{ "Blah", new List<string>(new[] { "Something", "Dictionary1" })},
{ "Blah, blah", new List<string>(new[] { "Something else", "Dictionary2" }) }
};
private static readonly Dictionary<string, string> Dictionary1 = new Dictionary<string, string>
{
{ "G", "Green"},
{ "A", "Amber"},
{ "R", "Red"}
};
private static readonly Dictionary<string, string> Dictionary2 = new Dictionary<string, string>
{
{ "B", "Blue"},
{ "P", "Purple"}
};
private string Test()
{
var shouldBeSomething = TestDictionary["Blah"][0];
var shouldBeDictionary1 = TestDictionary["Blah"][1];
return shouldBeDictionary1["G"] // returned value should be Green
//MethodInfo methodInfo = typeof(Dictionary<string, string>).GetMethod(shouldBeDictionary1)
}
在上面的示例中尝试使用Reflection时,methodInfo返回null,因此无法使用Invoke。
任何建议将不胜感激。
当然可以,将对字典的引用存储在自己的字典中:
var dictionaryIndex = new Dictionary<string,Dictionary<string,string>>{
["Dictionary1"] = new Dictionary<string, string>
{
{ "G", "Green"},
{ "A", "Amber"},
{ "R", "Red"}
},
["Dictionary2"] = new Dictionary<string, string>
{
{ "B", "Blue"},
{ "P", "Purple"}
}
};
var shouldBeSomething = TestDictionary["Blah"][0];
var shouldBeDictionary1 = TestDictionary["Blah"][1];
return dictionaryIndex[shouldBeDictionary1]["G"] // returned value should be Green
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句