[英]How to create List of open generic type of class<T>?
i am working on someone else code i need to add few things i have a class我正在研究其他人的代码我需要添加一些东西我有一个 class
public abstract class Data<T>
{
}
public class StringData : Data<string>
{
}
public class DecimalData : Data<decimal>
{
}
in my program i want to maintain list of different type of data在我的程序中,我想维护不同类型数据的列表
List<Data> dataCollection=new List<Data>();
dataCollection.Add(new DecimalData());
dataCollection.Add(new stringData());
List<Data> dataCollection=new List<Data>();
at above line i am getting compiler error Using the generic type 'Data' requires 1 type arguments在上面的行中,我收到编译器错误使用通用类型“数据”需要 1 种类型 arguments
Can any one guide what i am doing wrong;谁能指导我做错了什么?
There is no diamond operator in C# yet, so you can't use true polymorphism on open generic type underlying to closed constructed types. C# 中还没有菱形运算符,因此您不能在封闭构造类型基础的开放泛型类型上使用真正的多态性。
So you can't create a list like this:所以你不能创建这样的列表:
List<Data<>> list = new List<Data<>>();
You can't use polymorphism on such list... and it is a lack in genericity here.你不能在这样的列表上使用多态性......这里缺乏通用性。
For example, in C# you can't create a List<Washer<>>
instance to have some Washer<Cat>
and some Washer<Dog>
to operate Wash()
on them...例如,在 C# 中,您不能创建一个
List<Washer<>>
实例来让一些Washer<Cat>
和一些Washer<Dog>
对它们操作Wash()
...
All you can do is using a list of objects or an ugly non generic interface pattern:您所能做的就是使用对象列表或丑陋的非通用接口模式:
public interface IData
{
void SomeMethod();
}
public abstract class Data<T> : IData
{
public void SomeMethod()
{
}
}
List<IData> list = new List<IData>();
foreach (var item in list)
item.SomeMethod();
You can also use a non generic abstract class instead of an interface:您还可以使用非通用抽象 class 代替接口:
public abstract class DataBase
{
public abstract void SomeMethod();
}
public abstract class Data<T> : DataBase
{
public override void SomeMethod()
{
}
}
List<DataBase> list = new List<DataBase>();
foreach (var item in list)
item.SomeMethod();
But you lost some genericity design and strong-typing...但是你失去了一些通用性设计和强类型......
And you may provide any non-generic behavior such as properties and methods you need to operate on.您可以提供任何非泛型行为,例如您需要操作的属性和方法。
Generics open and closed constructed types Generics 开闭构造型
C# variance problem: Assigning List<Derived> as List<Base> C# 方差问题:将 List<Derived> 分配为 List<Base>
How to do generic polymorphism on open types in C#? 如何对 C# 中的开放类型进行泛型多态性?
C#: Is Variance (Covariance / Contravariance) another word for Polymorphism? C#:方差(协方差/逆变)是多态性的另一个词吗?
C# generic inheritance and covariance part 2 C# 通用 inheritance 和协方差第 2 部分
still confused about covariance and contravariance & in/out 仍然对协变和逆变和输入/输出感到困惑
Covariance and Contravariance (C#) 协变和逆变 (C#)
You don't need to use generics for that.您不需要为此使用 generics 。
public abstract class Data {
}
public class StringData : Data {
}
public class DecimalData : Data {
}
List<Data> dataCollection = new List<Data>();
dataCollection.Add(new DecimalData());
dataCollection.Add(new StringData());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.