为什么 new() 约束必须需要公共构造函数?
免责声明:理论问题
新的约束指定任何 泛型类中的类型参数 声明必须公开 无参数构造函数。
来源:http://msdn.microsoft .com/en-us/library/sd2w2ew5(v=vs.80).aspx
如果我希望我的泛型类具有一个受保护无参数构造函数,该怎么办?例如,如果我想编写一个 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()
{
}
}
这样,Singleton
就能够创建 OnlyOneOfMe
类的唯一实例,但其他任何东西都不能(除非它是子类)。
“如果泛型父类可以访问泛型类型的受保护成员怎么办?”
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
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.
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).
"What if a generic parent class could access the generic type's protected members?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为这就是约束的定义。这有点像问为什么
T : class
要求T
是引用类型。根据定义,这是真的。此外,如果它不是公共构造函数,那么约束的意义何在?如果构造函数不是公共的,则接收类型参数
T
的类将无法调用该构造函数。Because that is the definition of the constraint. It's a bit like asking why does
T : class
require thatT
be a reference type. 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.您可以使用 reflection 调用受保护的构造函数。然而,这应该会引起警告信号,表明您正在做一些不应该做的事情。在大多数情况下,您应该能够避免单例并使用依赖注入 相反。如果这也不起作用,您可以使用环境上下文模式之类的东西(请在此处查看我的答案)。
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).
.NET 不会知道您不想接受
作为有效类。由于它实际上是有效的,因此它将尝试创建该类并需要一个公共的其他构造函数。
.NET would not know that you don't want to accept
as a valid class. Since it is actually valid it will try to make the class and needs a public Other constructor.
如果构造函数受到保护,Singleton 将无法调用它。
无论如何,我都会避免实现这样的单例模式,即使我可以。这很混乱 - 如果您想要一个继承抽象类的单例类怎么办?
If the constructor were protected, Singleton wouldn't be able to call it.
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?