简体   繁体   English

C#泛型 - 类型参数的约束

[英]C# Generics - Constraints on type parameters

I'm trying to build a factory method that uses the generics feature of C#. 我正在尝试构建一个使用C#的泛型功能的工厂方法。

In this factory method I would like to constraint it to some specific classes, all of which do not have a default constructor. 在这个工厂方法中,我想将它约束到一些特定的类,所有这些类都没有默认的构造函数。

Here is my example. 这是我的例子。 Can someone tell me if it's possible to run it? 有人能告诉我是否可以运行它?

public class AbstractClass {
    //this abstract class does not have a default constructor, nor its subclasses
    public AbstractClass(SomeClassName obj) {
        //use obj for initialization
    }
}

//this factory class should create objects of type T that inherit 
//from AbstractClass and invoke the non-default constructor
public class FactoryClass {
    public static T BuildObject<T> (SomeClassName obj) where T: AbstractClass {
        return new T(obj); //does not work?!?!?!
    }
}


//Edit: ANSWER!!!
public static T BuildObject<T>(SomeClassUsedForTheConstructor item) where T : SomeAbstractClass { 
return (T) Activator.CreateInstance(typeof (T), item); 
} 

我喜欢在我的泛型中使用Activator.CreateInstance(typeof(T)) ,需要创建类型为T的新对象。它非常有效。

Look at the Type class and GetConstructor. 查看Type类和GetConstructor。 Once you get the ConstructorInfo object, use the Invoke Method. 获得ConstructorInfo对象后,使用Invoke方法。

var x = typeof(T);
var t = x.GetConstructor(new[] {obj.GetType()});
object u = t.Invoke(<inputs>);

I don't think you can instantiate generic types without a default constructor on the constraint type. 我不认为你可以在约束类型上没有默认构造函数的情况下实例化泛型类型。

Consider instead specifying an interface IAbstractClass, such that your factory class can set the SomeClassName parameter as a property of IAbstractClass. 请考虑指定接口IAbstractClass,以便您的工厂类可以将SomeClassName参数设置为IAbstractClass的属性。

Additionally, if a SomeClassName instance is required for initializing AbstractClass, consider also having an empty default constructor, but a rich initializer method defined in IAbstractClass. 此外,如果初始化AbstractClass需要SomeClassName实例,请考虑使用空的默认构造函数,但在IAbstractClass中定义的富初始化方法。 For example: 例如:

public interface IAbstractClass { void Initialize(SomeClassName obj); }

That way, your static BuildObject method instead does: 这样,您的静态BuildObject方法改为:

public static T BuildObject<T>(SomeClassName obj) where T: AbstractClass 
{
  T newObject = new T();
  IAbstractClass ac = newObject as IAbstractClass;
  ac.Initialize(obj);
}

No, what you are trying to do is not possible using the built-in generic constraints alone. 不,仅仅使用内置的通用约束是不可能的。 The new keyword only allows you to constrain the generic type to having a default constructor. new关键字仅允许您将泛型类型约束为具有默认构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM