有两个键和一个对象的最佳C#集合是什么?

Edw*_*uay 7 c# collections wpf prism

我有一个MenuManager类,每个模块可以添加一个键和要加载到主要内容中的元素:

private Dictionary<string,object> _mainContentItems = new Dictionary<string,object>();
public Dictionary<string,object> MainContentItems
{
    get { return _mainContentItems; }
    set { _mainContentItems = value; }
}
Run Code Online (Sandbox Code Playgroud)

因此,客户模块会像这样注册其视图:

layoutManager.MainContentViews.Add("customer-help", this.container.Resolve<HelpView>());
layoutManager.MainContentViews.Add("customer-main", this.container.Resolve<MainView>());
Run Code Online (Sandbox Code Playgroud)

所以后来我给前面带了一个特定的视图我说:

layoutManager.ShowMainContentView("customer-help");
Run Code Online (Sandbox Code Playgroud)

为了获得默认视图(第一个注册视图),我说:

layoutManager.ShowDefaultView("customer");
Run Code Online (Sandbox Code Playgroud)

这很好用.

但是,我想用连字符消除"代码味道",它将模块名称和视图名称分开,所以我想注册这个命令:

layoutManager.MainContentViews.Add("customer","help", this.container.Resolve<HelpView>());
Run Code Online (Sandbox Code Playgroud)

但是更换我当前词典的最佳方法是什么,例如我想到的是:

  • Dictionary<string, string, object> (doesn't exist)
  • Dictionary<KeyValuePair<string,string>, object>
  • Dictionary<CUSTOM_STRUCT, object>

新的集合需要能够做到这一点:

  • 获取模块和视图键的视图(例如"customer","help"返回1视图)
  • 按模块键获取所有视图的集合(例如"customer"返回5个视图)

Ste*_*ham 13

严格符合您的标准,使用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;
Run Code Online (Sandbox Code Playgroud)