C# 如何使用字典来引用其他字典?

Som*_*les 4 c# dictionary

根据我在其他地方读到的内容,一般建议似乎是使用字典来动态访问变量/对象和其他字典……但是我似乎缺少最后一种情况的简单内容,因为我看不到如何获得它上班。基本上我有多个数据字典,我希望使用变量中的值指向适当的字典并读取其数据:

//----------------------------------------------------------------------------------------

// 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";
Run Code Online (Sandbox Code Playgroud)

//...但现在我该怎么办???

Tim*_*ter 8

你是问如何访问字典吗?如下所示:

var text = myDictionaries["EN"]["str1"];
Run Code Online (Sandbox Code Playgroud)

你需要像这样定义你的字典:

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 }
    };
Run Code Online (Sandbox Code Playgroud)