Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#泛型-实现其他泛型类的类_C#_Generics - Fatal编程技术网

C#泛型-实现其他泛型类的类

C#泛型-实现其他泛型类的类,c#,generics,C#,Generics,我有一个 public class A<T> where T : IBase { //Does something } 公共A类,其中T:IBase { //做点什么 } 我需要第二个类,其行为类似于类a的集合 public class B<A<T>> : IEnumerable<A<T>> where T : IBase { } 公共类B:IEnumerable其中T:IBase { } 问题是我不想创建像这样的类 p

我有一个

public class A<T> where T : IBase
{
    //Does something
}
公共A类,其中T:IBase
{
//做点什么
}
我需要第二个类,其行为类似于类a的集合

public class B<A<T>> : IEnumerable<A<T>> where T : IBase
{
}
公共类B:IEnumerable其中T:IBase
{
}
问题是我不想创建像这样的类

public class B<A<MyCustomObjectP>> : IEnumerable<A<MyCustomObjectP>>
{
}

public class C<A<MyCustomObjectQ>> : IEnumerable<A<MyCustomObjectQ>>
{
}
公共类B:IEnumerable
{
}
公共类C:IEnumerable
{
}
等等。。我想让CustomObject成为实现IBase的泛型类型参数

我发现即使这样做也是违法的:

public class B<T, U> : IEnumerable<T> where T : A<U> where U : IBase
{
}
公共类B:IEnumerable其中T:A其中U:IBase
{
}

如果这是非法的,我怎么能做出这种行为?是否有更好的排序设计模式可能会有所帮助?

IBase约束是在
a
上定义的,因此必须在所有希望使用
a
的泛型类上重新定义它(使用
U
来区分
a
类定义中的
T
,但它可以被称为任何东西)。您应该能够简单地执行以下操作:

public class B<T> : IEnumerable<A<T>> where T : IBase { ... }
public类B:IEnumerable其中T:IBase{…}

IBase约束是在
A
上定义的,因此必须在所有希望使用
A
的泛型类上重新定义它(使用
U
来区分
A
类定义中的
T
,但它可以被称为任何东西)。您应该能够简单地执行以下操作:

public class B<T> : IEnumerable<A<T>> where T : IBase { ... }
public类B:IEnumerable其中T:IBase{…}
您写道,您需要第二个类,其行为类似于类
a
的集合

由于您还有从
IBase
继承的其他类(如
B
),需要添加这些类,因此可以将该集合设置为
IBase
的集合

因此,解决方案看起来是这样的(请注意,我使用了
List
,但您可以很容易地用
IEnumerable
-替换它,但是您必须实现类似
。自己添加
的方法):

void Main()
{
var items=new CollectionOf();//创建IBase元素列表
Add(newa(){myProperty=“Hello”});//创建A的对象并将其添加到列表中
Add(new B(){myProperty=“World”});//创建B的对象并将其添加到列表中
foreach(项目中的var项目)
{
Console.WriteLine(item.myProperty);
}
}
//这是你要求的收集课程
公共类集合:列表
你在哪里:伊巴斯
{
//集合类枚举
//注意,您可以使用IEnumerable而不是List
}
公共A类:IBase
{
//实现IBase的类
公共字符串myProperty{get;set;}
}
公共B类:IBase
{
//类B也实现了IBase
公共字符串myProperty{get;set;}
}
公共接口IBase{
//一些介面
字符串myProperty{get;set;}
}
您写道,您需要第二个类,其行为类似于类
a
的集合

由于您还有从
IBase
继承的其他类(如
B
),需要添加这些类,因此可以将该集合设置为
IBase
的集合

因此,解决方案看起来是这样的(请注意,我使用了
List
,但您可以很容易地用
IEnumerable
-替换它,但是您必须实现类似
。自己添加
的方法):

void Main()
{
var items=new CollectionOf();//创建IBase元素列表
Add(newa(){myProperty=“Hello”});//创建A的对象并将其添加到列表中
Add(new B(){myProperty=“World”});//创建B的对象并将其添加到列表中
foreach(项目中的var项目)
{
Console.WriteLine(item.myProperty);
}
}
//这是你要求的收集课程
公共类集合:列表
你在哪里:伊巴斯
{
//集合类枚举
//注意,您可以使用IEnumerable而不是List
}
公共A类:IBase
{
//实现IBase的类
公共字符串myProperty{get;set;}
}
公共B类:IBase
{
//类B也实现了IBase
公共字符串myProperty{get;set;}
}
公共接口IBase{
//一些介面
字符串myProperty{get;set;}
}

Heh。这很简单。我想得太多了。谢谢这似乎奏效了。:)呵呵。这很简单。我想得太多了。谢谢这似乎奏效了。:)