我有一个 MenuManager 类,每个模块都可以向其中添加一个键和要加载到主要内容中的元素:
private Dictionary<string,object> _mainContentItems = new Dictionary<string,object>();
public Dictionary<string,object> MainContentItems
{
get { return _mainContentItems; }
set { _mainContentItems = value; }
}
所以客户模块像这样注册它的 View :
layoutManager.MainContentViews.Add("customer-help", this.container.Resolve<HelpView>());
layoutManager.MainContentViews.Add("customer-main", this.container.Resolve<MainView>());
所以稍后我会说一个特定的观点:
layoutManager.ShowMainContentView("customer-help");
为了获得默认 View (注册的第一个 View ),我说:
layoutManager.ShowDefaultView("customer");
这很好用。
但是,我想用分隔模块名称和 View 名称的连字符消除“代码味道”,所以我想用这个命令注册:
layoutManager.MainContentViews.Add("customer","help", this.container.Resolve<HelpView>());
但是替换我当前的词典的最佳方法是什么,例如想到的是这些:
-
Dictionary<string, string, object> (doesn't exist)
-
Dictionary<KeyValuePair<string,string>, object>
-
Dictionary<CUSTOM_STRUCT, object>
新的集合需要能够做到这一点:
- 获取包含模块和 View 键的 View (例如“客户”、“帮助”返回 1 个 View )
- 通过模块键获取所有 View 的集合(例如“客户”返回 5 个 View )
最佳答案
严格满足您的标准,使用 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# - 具有两个键和一个对象的最佳 C# 集合是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1171838/