C# - 多泛型类型在一个列表 [英] C# - Multiple generic types in one list
本文介绍了C# - 多泛型类型在一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这大概是不可能的,但我有这个类:
公共类的元数据和LT;数据类型>其中,数据类型:结构
{
私人数据类型mDataType;
}
还有更多的,但让我们保持它的简单。泛型类型(数据类型)的WHERE语句限制为值类型。我想要做的是有不同的类型(数据类型),这些元数据对象的列表。如:
列表<&元数据GT; metadataObjects;
metadataObjects.Add(新的元数据与所述; INT>());
metadataObjects.Add(新的元数据<布尔>());
metadataObjects.Add(新的元数据<双>());
这甚至可能吗?
解决方案
公共抽象类的元数据
{
}公共类的元数据和LT;数据类型> :元数据,其中数据类型:结构
{
私人数据类型mDataType;
}
This is probably not possible, but I have this class:
public class Metadata<DataType> where DataType : struct
{
private DataType mDataType;
}
There's more to it, but let's keep it simple. The generic type (DataType) is limited to value types by the where statement. What I want to do is have a list of these Metadata objects of varying types (DataType). Such as:
List<Metadata> metadataObjects;
metadataObjects.Add(new Metadata<int>());
metadataObjects.Add(new Metadata<bool>());
metadataObjects.Add(new Metadata<double>());
Is this even possible?
解决方案
public abstract class Metadata
{
}
public class Metadata<DataType> : MetaData where DataType : struct
{
private DataType mDataType;
}
这篇关于C# - 多泛型类型在一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文