【问题标题】:Get all types under each category using enum in c#在c#中使用枚举获取每个类别下的所有类型
【发布时间】:2014-07-18 10:38:48
【问题描述】:

我有 2 个枚举

 enum Categories{
        Entertainment=100,
        Food=200,
        Medical=300
        };


 enum Types
        {
            Multiplex = 101,
            GameZone,
            Bar = 201,
            Hotel,
            Cafe,
            Hospital = 301,
            Clinic 
        };

我想列出特定类别下的所有类型 例如。 如果我将娱乐作为输入输出列表将包含 Multiplex 和 Gamezone

我应该怎么做?

【问题讨论】:

  • 为什么要使用枚举来存储关系数据?
  • 这看起来是一种非常糟糕的方式来关联这些数据。
  • 它们之间的关系如何?请看When to use enums
  • 我们可以使用结构吗
  • 您需要将行为与不同的类型或类别联系起来吗? IE。所有娱乐类型都指定可以服务的人数,或者所有咖啡馆都有菜单,或者类似的东西,或者这更像是一个类别+子类别(你的问题中的类型),你只需要“标记" 其他一些数据?

标签: c# enums


【解决方案1】:

我建议您使用字典,如下所示:

Dictionary<Categories,List<Types>> dictionary = new Dictionary<Categories, List<Types>>()
{
    { Categories.Entertainment, new List<Types> { Types.Multiplex , Types.GameZone} },
    { Categories.Food, new List<Type> { Types.Bar, Types.Hotel, Types.Cafe }}
};

这样你可以检索相应的列表,给出正确的键,如下所示:

dictionary[Categories.Entertainment]

将返回包含元素的列表

Types.MultiplexTypes.GameZone

【讨论】:

    【解决方案2】:

    首先,这看起来是一个非常糟糕的设计。你有隐含的关系,而不是显式的关系(即字典或类似类型的明确表示什么与什么相配)。

    我会认真考虑找到一种不同的方式来组织这些事情。

    但是,尽管如此,这里有一种获取类型的方法:

    var cat = Categories.Entertainment;
    
    var types =
        from Types type in Enum.GetValues(typeof(Types))
        where (int)type >= (int)cat && (int)type < (int)cat+100
        select type;
    

    【讨论】:

      【解决方案3】:

      我建议使用自定义属性来装饰类型枚举值,将其指向类别。

      我了解您已输入范围值,但我更喜欢自定义属性。

      例子

      带属性

      public class CategoryAttribute : Attribute
          {
              private readonly Category _category;
      
              public Category Category
              {
                  get
                  {
                      return _category;
                  }
              }
      
              public CategoryAttribute(Category category)
              {
                  _category = category;
              }
          }
      

      你可以有一些类似的方法

      public static Category GetCateogryFromType(Types categoryType)
              {
                  var memberInfo = typeof(Types).GetMember(categoryType.ToString())
                                                            .FirstOrDefault();
      
                  if (memberInfo != null)
                  {
                      var attribute = (CategoryAttribute)memberInfo.GetCustomAttributes(typeof(CategoryAttribute), false).FirstOrDefault();
                      if (attribute != null)
                      {
                          return attribute.Category;
                      }
                  }
      
                  throw new ArgumentException("No category found");
              }
      
              public static IEnumerable<Types> GetCategoryTypes(Category category)
              {
                  var values = Enum.GetValues(typeof(Types)).Cast<Types>();
                  return values.Where(t => GetCateogryFromType(t) == category);
              }
      

      然后像这样装饰你的类型

      public enum Types
          {
              [Category(Category.Entertainment)]
              Multiplex,
              [Category(Category.Entertainment)]
              GameZone,
              [Category(Category.Food)]
              Bar,
              [Category(Category.Food)]
              Hotel,
              [Category(Category.Food)]
              Cafe,
              [Category(Category.Medical)]
              Hospital,
              [Category(Category.Medical)]
              Clinic
          }
      

      那你就可以打电话了

      GetCategoryTypes(Category.Entertainment).ToList();
      

      哦,我将您的 Categories 枚举重命名为 Category :-p

      【讨论】:

        猜你喜欢
        • 2017-01-14
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多