什么是最好的C#集合了两个按键,一个对象呢? [英] What is the best C# collection with two keys and an object?
问题描述
我有一个 MenuManager 类,每个模块可以添加一个键和元素被加载到的主要内容:
I've got a MenuManager class to which each module can add a key and and element to be loaded into the main content:
private Dictionary<string,object> _mainContentItems = new Dictionary<string,object>();
public Dictionary<string,object> MainContentItems
{
get { return _mainContentItems; }
set { _mainContentItems = value; }
}
因此,客户模块注册这样的看法:
So the customer module registers its views like this:
layoutManager.MainContentViews.Add("customer-help", this.container.Resolve<HelpView>());
layoutManager.MainContentViews.Add("customer-main", this.container.Resolve<MainView>());
所以后来把在前面特定视图我说:
So then later to bring a specific view to the front I say:
layoutManager.ShowMainContentView("customer-help");
和获取默认的视图(第一个视图注册),我说:
And to get the default view (first view registered), I say:
layoutManager.ShowDefaultView("customer");
和这工作得很好。
不过,我想消除代码味道与分离模块名称和视图名称的连字符,所以我想用这个命令来注册:
However, I want to eliminate the "code smell" with the hyphen which separates module name and view name, so I would like to register with this command:
layoutManager.MainContentViews.Add("customer","help", this.container.Resolve<HelpView>());
但是,什么是替代我目前字典的最佳方式,例如想到什么是这些:
-
词典<字符串,字符串对象> (不存在)
-
词典< KeyValuePair<字符串,字符串>中对象>
-
词典< CUSTOM_STRUCT,对象>
Dictionary<string, string, object> (doesn't exist)
Dictionary<KeyValuePair<string,string>, object>
Dictionary<CUSTOM_STRUCT, object>
新系列需要能够做到这一点:
The new collection needs to be able to do this:
- 获得与模块和浏览键(如客户视图,帮助返回1视图)
- 获得通过模块键中的所有意见的集合(如客户返回5次)
推荐答案
严格符合您的标准,使用词典<字符串,字典<字符串对象>>
;
Strictly meeting your criteria, use a Dictionary<string, Dictionary<string, object>>
;
var dict = new Dictionary<string, Dictionary<string, object>>();
...
object view = dict["customer"]["help"];
Dictionary<string, object>.ValueCollection views = dict["customer"].Values;
这篇关于什么是最好的C#集合了两个按键,一个对象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!