是否可以限制实现接口的类的构造函数?
是否可以设置一个约束,即所有实现接口的类都必须具有(例如,空的构造函数)?就像泛型中的 where T : new()
约束一样?
Is it possible to set a constrain that all classes implementing an interface must have, for example, an empty constructor? Like the where T : new()
constraint in generic ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你需要为此使用虚拟类。
I think you need to use a virtual class for that.
正如 Justin 所说,不仅不能使用接口来约束构造函数签名,而且也不可能使用抽象类。
也许如果您能解释为什么需要设置这样的限制,我们可以为您的问题找到一些其他解决方案
As Justin said not only you can't constrain constructor signatures using an interface but also it's not possible using an abstract class.
Maybe if you would explain why you need to place such a constrain we could find some other solutions for your problem
将工厂注入到可以实例化接口的泛型类中,并删除 new() 约束。
像这样的东西:
Inject a Factory into your generic class that can instantiate your interface, and drop the new() constraint.
Something like:
不 - 不可能对给定接口的派生类或实现者施加任何此类约束。
无论如何,这样的约束通常不是一个特别好的主意/有用的,因为通常在使用接口时,您通常会使用实现该接口的对象实例,在这种情况下,对象自然已经被创建,并且这样的约束是多余的。 (当然泛型是例外,在这种情况下您可以使用
new()
约束)。我的猜测是,您正在尝试创建某种插件系统,并希望限制插件接口的实现,以拥有一些可用于实例化的默认构造函数...如果是这种情况,那么通常有更好的替代方案可以使用,例如MEF。
您能详细说明为什么您需要这个吗?
No - its not possible to place any such constraints on derived classes or implementors of a given interface.
Such constrains generally wouldn't be a particularly good idea / useful anyway, as generally when working with an interface you are normally working with instances of objects that implement that interface in which case the object has naturally already been created and such constraints are redundant. (The exception of course being generics, in which case you can use the
new()
constraint).My guess is that you are attempting to create some sort of plugin system and wish to constrain implementations of your plugin interface to have some default constructor that you can use for instantiation... if this is the case then there are normally better alternatives that you can use, such as the MEF.
Can you elaborate more on why exactly you need this?
我能想到的只有四种方法可以在运行时为您提供一个在编译时未知的类。您可能会得到一个实现接口的对象实例,并且想要生成另一个类似的实例。最好通过让接口包含 NewSimilarInstance() 方法来处理这种情况。您可能在某个类中有一个方法,该方法会传递一个受限于您的接口的泛型类型参数。在这种情况下,接受通用参数的例程可能具有 new() 约束。否则,您可能会获得一个 .net
System.Type
对象或该类型的某种其他表示形式(例如字符串)。在后两种情况下,编译时验证没有任何意义;对类型执行任何操作都需要反射,因此您也可以使用反射来查看它们是否允许创建新实例。There are only four ways I can think of that you might be given a class at run-time which isn't known at compile-time. You might be given an instance of an object which implements an interface, and want to produce another one like it. That scenario would best be handled by having the interface include a
NewSimilarInstance()
method. You might have a method in some class which gets passed a generic type parameter which is constrained to your interface. In that scenario, the routine which accepts the generic parameter could have anew()
constraint. Otherwise, you could be given a .netSystem.Type
object or some other representation (such a string) for the type. In these latter two scenarios, no compile-time validation is going to be meaningful; doing anything with the types will require Reflection, and so you may as well use Reflection to see if they allow the creation of new instances.不,没有那样的事。这有点奇怪,因为接口的正常使用是使用接口的代码不需要关心它是如何实例化的 - 他们不应该关心实现类是什么,只关心它实现了接口。
如果您对此有一些特殊用途,我建议您只需为其编写单元测试 - 如果所有实现都在同一个程序集中,那么这样做应该非常简单,并且会在 nearly< 处捕获任何错误/em> 与编译时相同...
No, there's nothing like that. It would be slightly odd, given that the normal use of interfaces is that the code using an interface shouldn't need to care about how it was instantiated - they shouldn't care about what the implementation class is, just that it implements the interface.
If you have some special use for this, I suggest you just write unit tests for it - if all the implementations will be in the same assembly, it should be pretty straightforward to do so, and will catch any errors at nearly the same time as compile-time...