根据我在其他地方读到的内容,一般建议是使用字典来动态访问变量/对象和其他字典......但是,对于最后一种情况,我似乎缺少一些简单的东西,因为我看不到如何使其工作。基本上我有多个数据字典,我希望使用变量中的值指向相应的字典并读取其数据:
//----------------------------------------------------------------------------------------
// reference dictionary - pass LangID string to reference appropriate dictionary
public static Dictionary<string, dynamic> myDictionaries = new Dictionary<string, dynamic>()
{
{ "EN", "EN_Dictionary" },
{ "FR", "FR_Dictionary" },
{ "DE", "DE_Dictionary" }
};
//----------------------------------------------------------------------------------------
public static Dictionary<string, string> EN_Dictionary = new Dictionary<string, string>()
// EN language dictionary
{
{ "str1", "Some text in EN" },
{ "str2", "Some text in EN" },
{ "str3", "Some text in EN" }
};
//----------------------------------------------------------------------------------------
public static Dictionary<string, string> FR_Dictionary = new Dictionary<string, string>()
// FR language dictionary
{
{ "str1", "Some text in FR" },
{ "str2", "Some text in FR" },
{ "str3", "Some text in FR" }
};
//----------------------------------------------------------------------------------------
public static Dictionary<string, string> DE_Dictionary = new Dictionary<string, string>()
// DE language dictionary
{
{ "str1", "Some text in DE" },
{ "str2", "Some text in DE" },
{ "str3", "Some text in DE" }
};
//----------------------------------------------------------------------------------------
LangID = "DE";
//...但现在我该怎么办???
你在问如何访问字典吗? 具体如下:
var text = myDictionaries["EN"]["str1"];
你需要像这样定义你的字典:
public static Dictionary<string, string> EN_Dictionary = ...etc;
public static Dictionary<string, string> FR_Dictionary = ...etc;
public static Dictionary<string, string> DE_Dictionary = ...etc;
public static Dictionary<string, Dictionary<string, string>> myDictionaries
= new Dictionary<string, Dictionary<string, string>>()
{
{ "EN", EN_Dictionary },
{ "FR", FR_Dictionary },
{ "DE", DE_Dictionary }
};
我将动态更改为语言词典的实际定义,然后添加了变量而不是字符串。
// reference dictionary - pass LangID string to reference appropriate dictionary
public static Dictionary<string, Dictionary<string, string>> myDictionaries = new Dictionary<string, Dictionary<string, string>>()
{
{ "EN", EN_Dictionary },
{ "FR", FR_Dictionary },
{ "DE", DE_Dictionary }
};
public static Dictionary<string, string> EN_Dictionary = new Dictionary<string, string>()
{
{ "str1", "Some text in EN" },
{ "str2", "Some text in EN" },
{ "str3", "Some text in EN" }
};
public static Dictionary<string, string> FR_Dictionary = new Dictionary<string, string>()
// FR language dictionary
{
{ "str1", "Some text in FR" },
{ "str2", "Some text in FR" },
{ "str3", "Some text in FR" }
};
public static Dictionary<string, string> DE_Dictionary = new Dictionary<string, string>()
// DE language dictionary
{
{ "str1", "Some text in DE" },
{ "str2", "Some text in DE" },
{ "str3", "Some text in DE" }
};
要使用参考词典...
private void button1_Click(object sender, EventArgs e)
{
string LangID = "DE";
Dictionary<string, string> GermanDictionary = myDictionaries[LangID];
string PhraseID = "str2";
string GermanPhrase = GermanDictionary[PhraseID];
}
您可以使用字典字典。例:
Dictionary<String, Dictionary<String, String>>
密钥和数据类型可能会根据您的需要而有所不同。