7

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

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

所以客户模块注册它的视图是这样的:

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

所以稍后我会说:

layoutManager.ShowMainContentView("customer-help");

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

layoutManager.ShowDefaultView("customer");

这很好用。

但是,我想用分隔模块名称和视图名称的连字符消除“代码气味”,所以我想用这个命令注册:

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>

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

  • 获取带有模块和视图键的视图(例如“客户”、“帮助”返回 1 个视图)
  • 通过模块键获取所有视图的集合(例如“客户”返回 5 个视图)
4

3 回答 3

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;
于 2009-07-23T13:52:15.770 回答
4

正如在类似线程中提到的,在 .NET 4 中表示 2 键字典的一种好方法是使用Tuple类:

IDictionary<Tuple<K1, K2>, V>
于 2012-02-02T03:53:13.567 回答
1

您所描述的听起来像是字典中的复合键,而不是两个键。我建议设置一个简单的结构来表示这个键:

struct Section {
   string Area { get; set; } 
   string Area2 { get; set; }

   // override ToHashCode, Equals and implement IComparable.
}
于 2009-07-23T13:50:38.113 回答