如何使用C#的Dictionary引用其他的Dictionary?

4

根据我在其他地方阅读的内容,似乎一般建议使用字典来动态访问变量/对象和其他字典... 但是我似乎忽略了最后一种情况的简单方法,因为我不知道如何使其工作。基本上,我有多个数据字典,我希望使用变量中的值指向适当的字典并读取其数据:

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

// 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";

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


如果您有不相交的键,则此处无需进行两级嵌套。例如,DEstr1FRstr1等 - 键是两个字符串的连接 - 语言代码和实际键。 - Wiktor Zychla
更改顺序。将字典的字典定义为最后一件事。然后您将能够引用已定义的字典和下面的答案。 - matcheek
3个回答

8
你想知道如何访问字典吗?可以按照以下方式进行操作:
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 }
    };

谢谢您的反馈 - 这里有一个“但是”的问题 - 如果我想将字典名称作为引用传递到代码的另一部分,我该如何使用它来动态地提供对语言字典的引用? - Some Bad Examples
我不太确定你在这种情况下所说的“动态”。最好创建另一个问题。 - Tim Rutter

4

我将动态转换为语言字典的实际定义,然后添加了变量而不是字符串。

    // 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];
    }

谢谢 - 这些都非常有帮助。 - Some Bad Examples

3

您可以使用字典嵌套字典的方式。示例代码如下:

Dictionary<String, Dictionary<String, String>>

按需求而定,密钥和数据的类型可能会有所不同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接