gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:02:45 27 4
gpt4 key购买 nike

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

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

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

关于C# 如何使用一个字典引用其他字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59511311/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com