如何要求没有参数的构造函数实现接口的类型? [英] How can you require a constructor with no parameters for types implementing an interface?

查看:281
本文介绍了如何要求没有参数的构造函数实现接口的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有办法吗?

我需要实现一个特定接口的所有类型都有一个无参数的构造函数,可以吗?

I need all types that implement a specific interface to have a parameterless constructor, can it be done?

我正在开发我公司其他开发人员的基本代码,用于特定项目。

I am developing the base code for other developers in my company to use in a specific project.

有一个进程,类型(在不同的线程)执行某些任务,我需要这些类型遵循特定的合同(ergo,接口)。

There's a proccess which will create instances of types (in different threads) that perform certain tasks, and I need those types to follow a specific contract (ergo, the interface).

如果你有没有接口的场景的建议,我会很乐意考虑...

If you have a suggestion for this scenario without interfaces, I'll gladly take it into consideration...

推荐答案

Juan Manuel说:


这是我不明白的原因之一它不能是界面中的合同的一部分

that's one of the reasons I don't understand why it cannot be a part of the contract in the interface

这是一种间接机制。通用允许你欺骗和发送类型信息与接口。这里要记住的关键是,约束不在您直接使用的接口上。它不是对接口本身的约束,而是在接口上沿着的一些其他类型。这是我可以提供的最好的解释,恐怕。

It's an indirect mechanism. The generic allows you to "cheat" and send type information along with the interface. The critical thing to remember here is that the constraint isn't on the interface that you are working with directly. It's not a constraint on the interface itself, but on some other type that will "ride along" on the interface. This is the best explanation I can offer, I'm afraid.

为了说明这个事实,我会指出一个洞,我注意到在aku的码。有可能编写一个类,它将编译良好,但在运行时失败,当您尝试实例化:

By way of illustration of this fact, I'll point out a hole that I have noticed in aku's code. It's possible to write a class that would compile fine but fail at runtime when you try to instantiate it:

public class Something : ITest<String>
{
  private Something() { }
}

源自ITest< T> ;,但是不实现无参数构造器。它会编译正常,因为String确实实现了一个无参数的构造函数。再次,约束是在T,因此字符串,而不是ITest或东西。由于满足对T的约束,这将被编译。

Something derives from ITest<T>, but implements no parameterless constructor. It will compile fine, because String does implement a parameterless constructor. Again, the constraint is on T, and therefore String, rather than ITest or Something. Since the constraint on T is satisfied, this will compile. But it will fail at runtime.

为了防止这个问题的一些实例,您需要向T添加另一个约束,如下所示: / p>

To prevent some instances of this problem, you need to add another constraint to T, as below:

public interface ITest<T>
  where T : ITest<T>, new()
{
}


$ b b

注意新的约束:T:ITest< T> ;.此约束指定您传递到ITest< T>的argument参数中的内容。 必须也从< T>派生。

即使如此,强>孔的情况。下面的代码将编译正常,因为A有一个无参数的构造函数。但是因为B的无参构造器是私有的,所以用你的过程实例化B将在运行时失败。

Even so this will not prevent all cases of the hole. The code below will compile fine, because A has a parameterless constructor. But since B's parameterless constructor is private, instantiating B with your process will fail at runtime.

public class A : ITest<A>
{
}

public class B : ITest<A>
{
  private B() { }
}

这篇关于如何要求没有参数的构造函数实现接口的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆