[英]Is it possible to constrain generic types to Interface, new()?
I'm trying to create a simple object recycling class 我正在尝试创建一个简单的对象回收类
public class ObjectPool<T> where T : class, IRecyclable, new()
{
}
I want to be able to use it with my interface: 我希望能够在我的界面中使用它:
public interface ISomeInterface : IRecyclable
{
}
ObjectPool<ISomeInterface> pool = new ObjectPool<ISomeInterface>();
But this yields the error: 但这会产生错误:
error CS0310: The type `ISomeInterface' must have a public parameterless constructor in order to use it as parameter `T' in the generic type or method `ObjectPool<T>'
From what I have read online I know that I can't specify a constructor in an interface. 根据我在线阅读的内容,我知道我无法在界面中指定构造函数。
I have read that you can use reflection instead of "new" to create a new instance, though I'm concerned about the speed of performing this instantiation. 我已经读过你可以使用反射代替“new”来创建一个新实例,尽管我担心执行这个实例化的速度。
What is the correct way to resolve this situation? 解决这种情况的正确方法是什么? Is there a simpler solution that I'm completely overlooking?
有一个更简单的解决方案,我完全忽略了吗?
You can't provide an interface there. 您无法在那里提供界面。
class
and new
require it to be a constructable reference type. class
和new
要求它是可构造的引用类型。
Interfaces can only implement other interfaces. 接口只能实现其他接口。
interface IA : IB, IC
{
...
}
A good way to solve your dilemma is to introduce a factory interface as well. 解决困境的一个好方法是引入工厂界面。
interface IThing
{
...
}
interface IThingFactory
{
IThing Create();
}
Now anything that wants to have the ability to create things should receive an IThingFactory
for that purpose. 现在,任何想要有能力创建东西的东西都应该为此目的获得一个
IThingFactory
。
If you need a generic concept of a factory, you could use something like this: 如果您需要工厂的通用概念,可以使用以下内容:
interface IFactory<T>
{
T Create();
}
class ObjectPool<T, F>
where T : IRecyclable
where F : IFactory<T>
{
public ObjectPool(F factory)
{
...
}
}
You cannot construct an ObjectPool<ISomeInterface>
. 您无法构造
ObjectPool<ISomeInterface>
。 You could have a generic type MyClass<TT> where T:class,ISomeInterface,new()
declare an ObjectPool<TT>
within it, and then later on declare variables of type MyClass<SomeClassWhichImplementsISomeInterfaceAndHasADefaultConstructor>
, but the compiler can only execute methods for ObjectPool<T>
when T
is of a particular known class type which meets all the constraints. 你可以有一个泛型类型
MyClass<TT> where T:class,ISomeInterface,new()
声明一个ObjectPool<TT>
,然后在声明MyClass<SomeClassWhichImplementsISomeInterfaceAndHasADefaultConstructor>
类型的变量,但编译器只能执行方法ObjectPool<T>
当T
是满足所有约束的特定已知类类型时 。
Alternatively, you could omit the new
constraint and then require that any code which constructs an ObjectPool<T>
must pass the constructor (or other method that creates the instance) a Func<T>
. 或者,您可以省略
new
约束,然后要求构造ObjectPool<T>
任何代码必须将构造函数(或创建实例的其他方法)传递给Func<T>
。 That would make it possible to create an ObjectPool<ISomeInterface>
provided that one had a method which, when called, would return a new object of some suitable type that implemented ISomeInterface
. 这样就可以创建一个
ObjectPool<ISomeInterface>
前提是有一个方法,当被调用时,它将返回一个实现ISomeInterface
的某种合适类型的新对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.