使用具有无参数构造函数约束的泛型类型的接口?

发布于 2024-11-14 17:53:45 字数 576 浏览 2 评论 0原文

我有一个泛型类,例如 MyCollection,它需要其泛型类型 T 具有无参数构造函数。我有一个接口 IMyInterface,其中所有实现都具有无参数构造函数,但我无法向编译器告知这一事实,因此我无法使用 IMyInterface 作为类型参数 T。我该怎么办?

public class MyCollection<T> where T : new()
{
    bla bla ...
    T t = new T();
}

public interface IMyInterface
{
    bla bla ...
}
...
MyCollection<IMyInterface> x;   //Compile Time Error

我知道在 Interfacedefiningaconstructor-signature? 中提出了几乎相同的问题,但它已经两年了,我希望有人可以建议 C# 4.0 中的解决方法。

I have a generic class, say MyCollection<T>, that needs its generic type T have a parameterless constructor. I have an interface IMyInterface with all implementetations having parameterless contructors, but I cannot tell that fact to the compiler, so I cannot use IMyInterface as the type parameter T. What should I do?

public class MyCollection<T> where T : new()
{
    bla bla ...
    T t = new T();
}

public interface IMyInterface
{
    bla bla ...
}
...
MyCollection<IMyInterface> x;   //Compile Time Error

I know that almost the same question was asked in Interface defining a constructor signature? but it is two years old and I'm hoping maybe someone can suggest a workaround in C# 4.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海蓝天 2024-11-21 17:53:45

这是行不通的,因为 T t = new T(); 当接口为 T 时,它会变成 IMyInterface t = new IMyInterface(); ,这是完全无效的。你必须知道构造你的类型的具体实现是什么。不能将抽象类型或接口单独与 new 一起使用。如果 MyCollection 是您自己的类,如果您在构造函数中添加了一个参数,通过传入具体实现来在构造函数中设置 T t ,然后删除新的参数约束并使用该接口作为通用参数。

This won't work because T t = new T(); with the interface as T, it would become IMyInterface t = new IMyInterface(); which is completely invalid. You have to know what the concrete implementation to construct your type. You cannot use an abstract type or interface alone with new. If MyCollection is your own class if you added a parameter in the constructor to set T t in the constructor by passing in your concrete implementation and remove the new parameter constraint and use the interface as a generic parameter then.

怕倦 2024-11-21 17:53:45

处理必须能够构造泛型类型的事物但由于某种原因不能使用 New 约束的泛型类的另一种方法是将工厂传递给必须创建泛型类型的新对象的方法,或者要求作为接口的一部分,实现它的类提供创建另一个实例的方法。例如第一种用法,可以定义接口 IFactoryFromString。其中包括方法“T CreateFromString(String st)”,并将这样的工厂传递给需要能够创建给定字符串的 T 的例程。例如,对于第二种用法,充当树节点的接口可能包括创建另一个类似树节点的方法。第二种用法避免了对额外工厂类和参数的需要,但要求实际上有一个满足约束的类实例才能生成另一个工厂类和参数。

Another way to deal with generic classes that have to be able to construct things of the generic type but which cannot for whatever reason use the New constraint is to either pass a factory to the methods that will have to create new objects of the generic type, or require as part of the interface that classes implementing it provide a means of creating another instance. For example of the first usage, one might define an interface IFactoryFromString<T> which includes a method "T CreateFromString(String st)", and pass such a factory to a routine which needs to be able to create a T given a string. For example, For example of the second usage, an interface for something that behaves as tree node might include a method to create another similar tree node. The second usage avoids the need for additional factory classes and parameters, but requires that one actually have an instance of a class meeting a constraint in order to produce another one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文