【问题标题】:C# Dictionary with Named/Labeled Types具有命名/标记类型的 C# 字典
【发布时间】:2017-06-03 05:48:30
【问题描述】:

我几乎到处搜索,甚至不确定这是否可能,但是嘿,我想我会看看你们 C# 向导可能有什么解决方案或解决方法。

TL;DR:

我有一个使用 C# 字典的多维集合,并想指出字典中每个字符串的用途,如下所示:

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

这当然行不通。现在我只是在评论字典。

建议、想法、想法?

【问题讨论】:

  • 您可以编写一个自定义类,在内部使用该字典字典来存储数据,但使用更有意义的命名属性公开数据。

标签: c# dictionary collections types


【解决方案1】:

你不能这样做,但你可以添加一个摘要。

例如:

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

这些 cmets 将显示在智能感知中。


或者:

如果你想通过反射提取信息,你可以使用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;
    }
}

但智能感知会显示string

【讨论】:

  • 天哪!我从来不知道你可以做这样的别名。我最喜欢这种方法。很不错,很精彩。谢谢!
  • 在 VS2019 中,别名方法以智能感知方式显示名称,但如果您别名说两次 String,它将显示两者的第一个别名。
  • 摘要方法不起作用,因为它是无效的 XML。
【解决方案2】:

创建一个将键或值与注释配对的类:

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");
}

以上应该产生

Yes
No

因为AnnotatedVal("hello", "Area")AnnotatedVal("hello", "Controller") 使用不同的注解。

【讨论】:

    【解决方案3】:

    可以在 List 中使用Tuple Field Names

    private List<(string Area, List<(string Controller, string Action)>)> ActionCollection;
    

    这是 C# 7.0 或 .NET 4.3 通过导入 System.ValueTuple nuget 的功能。

    【讨论】:

    • 显然,这使用列表而不是字典,因此可能会影响性能。
    【解决方案4】:

    您可以将每个字符串包装在它自己的类中。那么声明和智能感知将是描述性的:

    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;
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 2013-09-25
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      相关资源
      最近更新 更多