简体   繁体   English

为什么new()约束必须要求公共构造函数?

[英]Why must the new() constraint require a public constructor?

Disclaimer: Theoretical Question 免责声明:理论问题

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor. 新约束指定泛型类声明中的任何类型参数都必须具有公共无参数构造函数。

Source: http://msdn.microsoft.com/en-us/library/sd2w2ew5(v=vs.80).aspx 资料来源: http//msdn.microsoft.com/en-us/library/sd2w2ew5(v=vs.80).aspx

What if I wanted my generic class to have a protected parameterless constructor instead? 如果我希望我的泛型类具有受保护的无参数构造函数,该怎么办? For instance, if I want to write a Singleton class which I "attach" to other classes to make them Singleton s, I don't want the derived classes to be instantiable - everything should go through the .Instance property. 例如,如果我想编写一个Singleton类,我将其“附加”到其他类以使它们成为Singleton ,我不希望派生类可以实例化 - 所有内容都应该通过.Instance属性。

internal class Singleton<T> where T : new()
{
    public static T Instance { get; private set; }

    static Singleton()
    {
        Singleton<T>.Instance = new T();
    }
}

internal class OnlyOneOfMe : Singleton<OnlyOneOfMe>
{
    protected OnlyOneOfMe()
    {
    }
}

This way, Singleton<T> is able to create the only instance of the OnlyOneOfMe class, but nothing else can (unless it is a subclass). 这样, Singleton<T>能够创建OnlyOneOfMe类的唯一实例,但没有其他任何东西可以(除非它是一个子类)。

"What if a generic parent class could access the generic type's protected members?" “如果通用父类可以访问泛型类型的受保护成员会怎么样?”

Because that is the definition of the constraint. 因为那是约束的定义。 It's a bit like asking why does T : class require that T be a reference type. 这有点像问为什么T : class要求T是引用类型。 It's true by definition. 根据定义,这是真的。

Additionally, if it weren't a public constructor, what would be the point of the constraint? 另外,如果它不是公共构造函数,那么约束的重点是什么? The class receiving the type parameter T wouldn't be able to call the constructor if it weren't public. 接收类型参数T的类如果不是公共的,则无法调用构造函数。

You can call a protected constructor using reflection . 您可以使用反射调用受保护的构造函数。 However this should raise warning signs that you are doing something you are not supposed to. 然而,这应该提出警告信号,表明你正在做一些你不应该做的事情。 In most cases, you should be able to avoid a singleton and use dependency injection instead. 在大多数情况下,您应该能够避免使用单例并使用依赖注入 If that doesn't work either, you can use something like the ambient context pattern ( see my answer here ). 如果这也不起作用,您可以使用环境上下文模式之类的东西( 请参阅我的答案 )。

If the constructor were protected, Singleton wouldn't be able to call it. 如果构造函数受到保护,Singleton将无法调用它。

And I'd avoid implementing the singleton pattern like that anyway, even if I could. 而且即使我可以,我也会避免像这样实现单例模式。 It's messy - what if you wanted a singleton class that inherits from an abstract one? 这很麻烦 - 如果你想要一个继承自抽象的单例类怎么办?

.NET would not know that you don't want to accept .NET不会知道你不想接受

class OnlyOneOfMe : Singleton<Other>

as a valid class. 作为有效的班级。 Since it is actually valid it will try to make the class and needs a public Other constructor. 由于它实际上是有效的,它将尝试创建类并需要一个公共的其他构造函数。

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

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