简体   繁体   English

如何声明包含泛型类型的数组?

[英]How to declare an array containing generic type?

Say I have the following type: 说我有以下类型:

public class Field<T>
{
  public string Name { get; set; }
  public T Value { get; set; }
}

How can I declare a variable that will contain an array of such Fields? 如何声明包含此类字段数组的变量? I tried the following: 我尝试了以下方法:

var customFields = new Field[]
{
    new Field<string> { Name = "nickname", Value = "Bob" },
    new Field<int> { Name = "age", Value = 42 },
    new Field<DateTime> { Name = "customer_since", Value = new DateTime(2000, 12, 1) }
};

but I get the following compile-time exception: 但我得到以下编译时异常:

Using the generic type 'Field' requires 1 type arguments 使用泛型类型“Field”需要1个类型参数

I also tried var customFields = new Field<object>[] but I get the following errors: 我也尝试了var customFields = new Field<object>[]但是我收到以下错误:

Cannot implicitly convert type 'Field' to 'Field' 无法隐式将“Field”类型转换为“Field”

Cannot implicitly convert type 'Field' to 'Field' 无法隐式将“Field”类型转换为“Field”

Cannot implicitly convert type 'Field' to 'Field' 无法隐式将“Field”类型转换为“Field”

As Matias said it can't be done that way because Field<int> and Field<string> are completely different types. 正如Matias所说,不能这样做,因为Field<int>Field<string>是完全不同的类型。 What you can do is: 你能做的是:

Create a base Field which is not generic: 创建一个非泛型的基本Field

public class Field
{
    public string Name { get; set; }
}

public class Field<T> : Field
{
    public T Value { get; set; }
}

And then: 接着:

var customFields = new Field[]
{
    new Field<string> { Name = "nickname", Value = "Bob" },
    new Field<int> { Name = "age", Value = 42 },
    new Field<DateTime> { Name = "customer_since", Value = new DateTime(2000, 12, 1) }
};

As Matias commented a downcasting will be needed to get the Value of the different types. 正如马蒂亚斯评论的那样,需要一个向下转型才能获得不同类型的Value If for each different type of Value there is a specific logic to perform, a nice way to handle it will be to use the Factory design pattern. 如果对于每种不同类型的Value都有一个特定的逻辑要执行,处理它的一个好方法是使用Factory设计模式。 The factory will return for each Field a correct " FieldHandler<T> " that will downcast, do the operation and return what is needed. 工厂将为每个Field返回一个正确的“ FieldHandler<T> ”,它将向下转换,执行操作并返回所需的内容。

Another thing you can do is to simply use the common base class for all of those types - Object. 您可以做的另一件事是简单地为所有这些类型使用公共基类 - 对象。

class Field
{
    public string Name { get; set; }
    public Object Value { get; set; }
}
var customFields = new Field[]
{
    new Field { Name = "nickname ", Value = "Bob" },
    new Field { Name = "age", Value = 42 },
    new Field { Name = "customer_since", Value = new DateTime(2000, 12, 1) }
};

However, this would be offloading some type handling to the consumer of customFields, but should be fine if the Value types associated with a specific Field Name are known. 但是,这会将某些类型处理卸载到customFields的使用者,但如果已知与特定字段名称关联的Value类型,则应该没问题。

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

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