2017-01-18 6 views
0

私はすべての場所を検索しましたが、それは可能ではありませんが、どういうことか、私はあなたが解決策や回避策のためにC#C#名前付き/ラベル付きの型を持つ辞書

TL; DR:

私はC#の辞書を使用して多次元のコレクションを持っているし、辞書内の各文字列が何のためにあるのかを示したい、このような何か:のコース

private Dictionary<string: Area, Dictionary<string: Controller, string: Action>> ActionCollection; 

動作しません。今のところ私は辞書にコメントしています。

提案、考え、アイデア?

+1

ディクショナリの辞書を内部的に使用してデータを格納するカスタムクラスを作成できますが、より意味のある名前付きプロパティでデータを公開します。 – juharr

答えて

3

これはできませんが、要約を追加することはできます。例えば

/// <summary> 
/// Dictionary<Area, Dictionary<Controller, Action>> 
/// </summary> 
private Dictionary<string, Dictionary<string, string>> ActionCollection; 

これらのコメントは、インテリセンスに表示されます。


または:

あなたが反射して情報を抽出したい場合は、あなたがcustom attributes


を使用することができ、それだけで読みやすくするためであれば、あなたはそれのためのエイリアスを作成することができます。

using Area = System.String; 
using Controller = System.String; 
using Action = System.String; 

namespace MyApp 
{ 
    public class MyClass 
    { 
     private Dictionary<Area, Dictionary<Controller, Action>> ActionCollection; 
    } 
} 

しかし、intellisenseはhow string

+0

ああああ!私はあなたがこのようなエイリアシングをすることができることを知らなかった。私はこのアプローチが一番好きです。非常に素晴らしい、非常に素晴らしい。ありがとうございました! – Vippy

0

各文字列を独自のクラスにラップすることができます。そして、宣言とインテリセンスが記述されます:

public class Area 
{ 
    public string area { get; set; } 
    public override string ToString() 
    { 
     return area; 
    } 
} 
public class Controller 
{ 
    public string controller { get; set; } 
    public override string ToString() 
    { 
     return controller; 
    } 
} 
public class Action 
{ 
    public string action { get; set; } 
    public override string ToString() 
    { 
     return action; 
    } 
} 
private Dictionary<Area, Dictionary<Controller, Action>> ActionCollection; 
0

クラスを作成することをペアキーまたは注釈付き値:

class AnnotatedVal { 
    public string Val {get;} 
    public string Annotation {get;} 
    public AnnotatedVal(string val, string annotation) { 
     // Do null checking 
     Val = val; 
     Annotation = annotation; 
    } 
    public bool Equals(object obj) { 
     var other = obj as AnnotatedVal; 
     return other != null && other.Val == Val && other.Annotation == Annotation; 
    } 
    public int GetHashCode() { 
     return 31*Val.GetHashCode() + Annotation.GetHashCode(); 
    } 
} 

private Dictionary<AnnotatedVal,Dictionary<AnnotatedVal,AnnotatedVal>> ActionCollection; 

は今、あなたは分離を確保するために、あなたの辞書にAnnotatedValを使用することができます。

ActionCollection.Add(new AnnotatedVal("hello", "Area"), someDictionary); 
if (ActionCollection.ContainsKey(new AnnotatedVal("hello", "Area"))) { 
    Console.WriteLine("Yes"); 
} else { 
    Console.WriteLine("No"); 
} 
if (ActionCollection.ContainsKey(new AnnotatedVal("hello", "Controller"))) { 
    Console.WriteLine("Yes"); 
} else { 
    Console.WriteLine("No"); 
} 

上記生成しなければならない

AnnotatedVal("hello", "Controller")は異なる注釈を使用しているため、

です。

関連する問題