假设我想指定在我的类中使用通用接口的实现。这是简单的(如果有点丑)如果类是用它来存储另一个公共类型:如何使用私有类型的自定义通用接口实现?
class Foo<T> where T : IList<Bar>, new()
{
private T _list = new T();
}
class Bar{}
然后我们就可以让富的新实例,因为这样:new Foo<List<Bar>>()
但会发生什么Bar
是内Foo
私有类:
class Foo<T> where T : IList<Bar>, new()
{
private T _list = new T();
class Bar{}
}
显然,这将失败,因为Foo
不能暴露在它的类型约束Bar
,而且也没有办法来实例化一个new Foo<List<Bar>>()
我会坚持揭露object
:
class Foo<T> where T : IList<object>, new()
{
private T _list = new T();
class Bar{}
}
但后来我从object
我每次使用接口时铸造Bar
。
这里我最好的选择是什么?
我不想暴露'酒吧'。我不想。我只需要在内部使用它,并使用传入的泛型类型。 – dlras2