你怎么能要求不带参数的类型实现接口的构造?你怎么、不带、接口、参数

2023-09-04 07:41:42 作者:夜半丶唱情歌

有没有办法?

我需要实现特定的接口有一个参数的构造函数所有类型的,能不能做到?

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

我开发基地code对其他开发人员在我的公司在具体项目中使用。

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...

推荐答案

胡安·曼努埃尔说:

这是原因之一,我不明白为什么它不能成为合同中的接口部分

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.

通过说明这一事实的方式,我会指出,我已经注意到,在阿克苏的code一个洞。这是可能的编写,将编译好的,但在运行时失败当您尝试实例化它的类:

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&LT; T&GT;但是实现无参数的构造函数。这将编译好的,因为串并实现参数的构造函数。同样,约束是对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.

要prevent 部分这个问题的情况下,你需要添加另一个约束T,如下:

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()
{
}

请注意,新的约束:T:ITEST&LT; T&取代。此约束指定您传递到ITEST 1所述的参数参数是什么; T&GT; 必须同时导出从ITEST&LT; T&GT;

Note the new constraint: T : ITest<T>. This constraint specifies that what you pass into the argument parameter of ITest<T> must also derive from ITest<T>.

即便如此,这会不会prevent 所有情况下,孔的。下面的code将编译好的,因为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() { }
}