简体   繁体   English

实现泛型类的集合

[英]Implementing collection of a generic class

I am trying to create a custom class like this as follows. 我正在尝试创建如下的自定义类。

public MyClass<T> 
{
    public string Value1 { get; set; }
    public T Value2 { get; set; }
    public string Value3 { get; set; }
}

The value of T could be either string or int or datetime .I assume i can create new instance of this class like T的值可以是string或int或datetime。我假设我可以创建此类的新实例,例如

MyClass<int> intclass = new MyClass<int>();
MyClass<String> stringclass=new MyClass<String>();

and so forth. 等等。

Is it possible to create a collection of the above classes where i can put intclass and stringclass into a custom collection. 是否可以创建上述类的集合,在其中我可以将intclass和stringclass放入自定义集合中。

If you want to mix different generic types (so have a collection containing both MyClass<int> and MyClass<string> ) you need to define some common base type or use a collection that is not strongly typed: 如果要混合使用不同的泛型类型(因此有一个包含MyClass<int>MyClass<string>的集合),则需要定义一些常见的基本类型使用不是强类型的集合:

public class MyClass<T> : MyClass
{
    public T Value2 { get; set; }
}

public class MyClass
{
    public string Value1 { get; set; }
    public string Value3 { get; set; }
}

Then you can define a collection like: 然后,您可以定义一个类似的集合:

List<MyClass> list = new List<MyClass>();
list.Add(new MyClass<int>());
list.Add(new MyClass<string>());

You will have to cast results when retrieving entries in order to access their Value2 property though. 但是,在检索条目时必须转换结果才能访问其Value2属性。

Another option to avoid the base-class is simply to use a List<object> : 避免基类的另一种选择是简单地使用List<object>

List<object> list = new List<object>();
list.Add(new MyClass<int>());
list.Add(new MyClass<string>());

But it's the same problem as above, but plausibly worse (because then you can store anything in there) 但这是与上述相同的问题,但可能更糟(因为您可以在其中存储任何内容

EDIT: There are various ways of how to allow untyped access to Value2 in the base non-generic MyClass . 编辑:有多种方法可以允许在基础非通用MyClassValue2进行无类型访问。 One way is to define an "untyped" version of it on the base class and override it on the subclass which would perform type-checking: 一种方法是在基类上定义它的“无类型”版本,并在执行类型检查的子类上覆盖它:

public abstract class MyClass
{
    public string Value1 { get; set; }
    public abstract object Value2Untyped { get; set; }
    public string Value3 { get; set; }
}

public class MyClass<T> : MyClass
{
    public T Value2 { get; set; }

    public override object Value2Untyped
    {
        get
        {
            return Value2;
        }
        set
        {
            Value2 = (T)value;
        }
    }
}

Then for all your objects or collections that are typed against the base non-generic MyClass , you can still access values, and even set values at runtime. 然后,对于根据基本非泛型MyClass键入的所有对象或集合,您仍然可以访问值,甚至可以在运行时设置值。 (the set of course is optional) (该set当然是可选的)

you mean something like this? 你的意思是这样的吗?

List<MyClass<int>> intClassList = new List<MyClass<int>>();
intClassList.Add(new MyClass<int>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM