使用枚举选择要实例化的类 [英] Use an enum to select which class to instantiate

查看:24
本文介绍了使用枚举选择要实例化的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举,我试图将它与 dto 关联:

I have an enum that I am trying to associate to dto's:

 public enum DtoSelection
 {
     dto1,
     dto2,
     dto3,
 }

这个枚举中有 108 个和值.

There are 108 and values in this enum.

对于这些 dto 中的每一个,我都有一个 dto 对象:

I have a dto object for each of these dto's:

 public class dto1 : AbstractDto
 {
       public int Id { get; set; }
       //some stuff specific to this dto
 }

我正在尝试创建一个方法(最终是一个服务),它将返回一个新的 dto 对象,该对象的类型与所讨论的 dto 相关联:

I am trying to make a method (eventually a service) that will return me a new dto object of the type associated to the the dto in question:

 private AbstractDto(int id)
 {
      if (id == DtoSelection.Dto1.ToInt()) //extension method I wrote for enums
            return new Dto1();
      if (id == DtoSelection.Dto2.ToInt())
            return new Dto2();
 }

显然我不想这样做 108 次.无论出于何种原因,我的大脑都缺少一些明显的东西.处理这个问题的最佳方法是什么.

Obviously I do not want to do this 108 times. For whatever reason my brain is just missing something obvious. What is the best way to handle this.

推荐答案

解决这个问题的一种优雅方式是使用属性和一个基类.让我告诉你:

An elegant way of solving this is by using Attributes and one base class. Let me show you:

  1. 您必须创建一个基类.在您的示例中,可以是 AbstractDto,如下所示:

  1. You must create a base class. In your example, could be AbstractDto, like following:

 public abstract class AbstractDto : Attribute
 {
      //code of AbstractDto       
 }

  • 然后,我们需要创建一个自定义属性,用于每个 Dto 类以确定哪个枚举对应于每个类.

  • Then, we need to create a custom attribute that will be used on every Dto class to determine which enum corresponds to each class.

     public class DtoEnumAttribute : Attribute
     {
         public DtoSelection Enum { get; set; }
    
         public DtoEnumAttribute(DtoSelection enum)
         {
             this.Enum = enum;
         }
      }
    

  • 然后我们应该用适当的枚举装饰每个子 Dto.我们以 Dto1 为例:

  • Then we should decorate every child Dto with its proper enum. Let's do an example for Dto1:

     [DtoEnum(DtoSelection.Dto1)]
     public class Dto1 : AbstractDto
     {
          //code of Dto1
     }
    

  • 最后,您可以使用一种方法来接收特定的枚举和过滤器,或者您需要的任何逻辑.以下代码将实例化从 AbstractDto 继承的每个类,这些类由您定义的 Enum 排序.您可以在 Where 子句上使用它来仅返回与您想要的枚举匹配的类的实例.问我在这方面您是否需要帮助.

  • Finally, you can use a method that can receive an specific enum and filter, or whatever logic you need. The following code will instantiate every class that inherit from AbstractDto ordered by the Enum that you have defined. You can use it on a Where clause to return only the instance of the class that matches the enum that you want. Ask me if you need help on this point.

     public void MethodToGetInstances()
     {
            IEnumerable<AbstractDto> dtos = typeof(AbstractDto)
                .Assembly.GetTypes()
                .Where(t => t.IsSubclassOf(typeof(AbstractDto)) && !t.IsAbstract)
                .Select(t => (AbstractDto)Activator.CreateInstance(t))
                .OrderBy(x => ((DtoEnumAttribute)x.GetType().GetCustomAttributes(typeof(DtoEnumAttribute), false).FirstOrDefault()).Enum);
    
            //If you have parameters on you Dto's, you might pass them to CreateInstance(t, params)
    
     }
    

  • dtos 列表中,您将拥有所需的实例.希望有帮助!

    On the dtos list, you will have the instances that you want. Hope it helps!

    这篇关于使用枚举选择要实例化的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆