C# - 一个列表中的多个泛型类型 [英] C# - Multiple generic types in one list

查看:28
本文介绍了C# - 一个列表中的多个泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是不可能的,但我有这门课:

This is probably not possible, but I have this class:

public class Metadata<DataType> where DataType : struct
{
    private DataType mDataType;
}

还有更多内容,但让我们保持简单.泛型类型 (DataType) 被 where 语句限制为值类型.我想要做的是列出这些不同类型(DataType)的元数据对象.如:

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>());

这甚至可能吗?

推荐答案

public abstract class Metadata
{
}

// extend abstract Metadata class
public class Metadata<DataType> : Metadata where DataType : struct
{
    private DataType mDataType;
}

这篇关于C# - 一个列表中的多个泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆