【问题标题】:How to pass an enum to a parent class's static function to instantiate a child class Part II?如何将枚举传递给父类的静态函数以实例化子类第二部分?
【发布时间】:2013-02-20 23:11:12
【问题描述】:

这是对之前question 的跟进。这个问题有一个错误(我实际上发布了我当前的解决方案,而不是我正在寻找的更好的解决方案)。

我有 3 个课程,ParentClass,ClassA,ClassB。 ClassA 和 ClassB 都是 ParentClass 的子类。我想尝试创建ClassA 或ClassB 类型的对象,使用某种枚举来标识类型,然后将对象实例化为父类型。我怎样才能动态地做到这一点?请看下面的代码,以及上面写着//what do I put here? 的部分。感谢阅读!

公共枚举类类型 { A类, B类 };

public abstract class ParentClass
{
    public static readonly Dictionary<ClassType, Type> Types = 
        new Dictionary<ClassType, Type>{
            {ClassType.ClassA, typeof(ClassA) },
            {ClassType.ClassB, typeof(ClassB) }
        };

    public ParentClass()
    {
        //....
    }

    public static ParentClass GetNewObjectOfType(ClassType type)
    {
        //What do I put here?
    }
}

public class ClassA:ParentClass
{
    //....
}
public class ClassB:ParentClass
{
    //.......
}

【问题讨论】:

标签: c#


【解决方案1】:

您可以使用Activator 类轻松完成此操作,尤其是在构造函数没有任何参数的情况下:

return Activator.CreateInstance(Types[type]);

【讨论】:

    【解决方案2】:

    重申您的问题:您正在寻找有关如何编写静态工厂方法的建议。

    我认为您不需要单独的枚举来完成此操作。这是我放在一起的 LINQPad 脚本(编辑:添加静态常量,使用它们创建演示)

    void Main()
    {
        var a = ParentClass.Create(typeof(ClassA));
        var b = ParentClass.Create(typeof(ClassB));
        var c = ParentClass.Create(ParentClass.ClassAType);
        a.Dump();
        b.Dump();
        c.Dump();
    }
    
    
    public abstract class ParentClass
    {
        public static readonly Type ClassAType = typeof(ClassA); 
        public static readonly Type ClassBType = typeof(ClassB);
        public string SomeProp { get; set; }
        protected ParentClass() {}
    
        public static ParentClass Create(Type typeToCreate)
        {
            // validate that type is derived from ParentClass
            return (ParentClass)Activator.CreateInstance(typeToCreate);
        }
    }
    
    public class ClassA:ParentClass {
        public ClassA()
        {
            SomeProp = "ClassA~";
        }
    
    }
    public class ClassB:ParentClass
    {
        public ClassB()
        {
            SomeProp = "ClassB~";
        }
    }
    

    【讨论】:

      【解决方案3】:

      如上一题所述:

      反射很慢,经常用在不需要的地方

      Activator.CreateInstance 使用反射来追踪无参数构造函数。要解决这个问题 - 这不是必需的。父类已经知道它负责创建的所有类型。


      使用静态工厂方法的原因之一是,创建子类可能涉及大量工作或不同种类的工作。我认为如果你加强你的地图,你会更容易编写静态工厂:

      public static readonly Dictionary<ClassType, Func<ParentClass>> Types = 
          new Dictionary<ClassType, Func<ParentClass>>{
              {ClassType.ClassA, () => new ClassA(1, 2, 3) },
              {ClassType.ClassB, () => new ClassB("1") }
          };
      
      
      public static ParentClass GetNewObjectOfType(ClassType type)
      {
          Func<ParentClass> maker = Types[type];
          ParentClass result = maker();
          return result;
      }
      

      【讨论】:

      • 我喜欢使用映射到委托的类型字典的技术 - 它允许一些美妙的组合,允许基于简单逻辑构建丰富的规则集
      猜你喜欢
      • 2013-02-06
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多