将不同类型的通用对象添加到通用列表中

发布于 2024-11-30 05:57:48 字数 606 浏览 1 评论 0原文

是否可以将不同类型的通用对象添加到列表中?如下。

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

假设我拥有所有这些对象...

 ValuePair<string> data1 =  new ValuePair<string>();
 ValuePair<double> data2 =  new ValuePair<double>();
 ValuePair<int> data3 =  new ValuePair<int>();

我想将这些对象保存在一个通用列表中。例如

List<ValuePair> list = new List<ValuePair>();

list.Add(data1);
list.Add(data2);
list.Add(data3);

是否可能?

Is it possible to add different type of generic objects to a list?. As below.

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

and let say I have all these objects...

 ValuePair<string> data1 =  new ValuePair<string>();
 ValuePair<double> data2 =  new ValuePair<double>();
 ValuePair<int> data3 =  new ValuePair<int>();

I would like to hold these objects in a generic list.such as

List<ValuePair> list = new List<ValuePair>();

list.Add(data1);
list.Add(data2);
list.Add(data3);

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2024-12-07 05:57:48

一般来说,您必须使用 List或创建一个非泛型基类,例如,

public abstract class ValuePair
{
    public string Name { get; set;}
    public abstract object RawValue { get; }
}

public class ValuePair<T> : ValuePair
{
    public T Value { get; set; }              
    public object RawValue { get { return Value; } }
}

然后您可以拥有 List

现在,有一个例外:C# 4 中的协变/逆变类型。例如,您可以编写:

var streamSequenceList = new List<IEnumerable<Stream>>();

IEnumerable<MemoryStream> memoryStreams = null; // For simplicity
IEnumerable<NetworkStream> networkStreams = null; // For simplicity
IEnumerable<Stream> streams = null; // For simplicity

streamSequenceList.Add(memoryStreams);
streamSequenceList.Add(networkStreams);
streamSequenceList.Add(streams);

这不适用于您的情况,因为:

  • 您使用的是泛型类,而不是接口
  • 您无法将其更改为通用协变接口,因为您已经让T“进”“出”接口API
  • 您正在使用值类型作为类型参数,并且这些不适用于泛型变量(因此 IEnumerable 不是 IEnumerable

In general, you'd have to either use a List<object> or create a non-generic base class, e.g.

public abstract class ValuePair
{
    public string Name { get; set;}
    public abstract object RawValue { get; }
}

public class ValuePair<T> : ValuePair
{
    public T Value { get; set; }              
    public object RawValue { get { return Value; } }
}

Then you can have a List<ValuePair>.

Now, there is one exception to this: covariant/contravariant types in C# 4. For example, you can write:

var streamSequenceList = new List<IEnumerable<Stream>>();

IEnumerable<MemoryStream> memoryStreams = null; // For simplicity
IEnumerable<NetworkStream> networkStreams = null; // For simplicity
IEnumerable<Stream> streams = null; // For simplicity

streamSequenceList.Add(memoryStreams);
streamSequenceList.Add(networkStreams);
streamSequenceList.Add(streams);

This isn't applicable in your case because:

  • You're using a generic class, not an interface
  • You couldn't change it into a generic covariant interface because you've got T going "in" and "out" of the API
  • You're using value types as type arguments, and those don't work with generic variable (so an IEnumerable<int> isn't an IEnumerable<object>)
因为看清所以看轻 2024-12-07 05:57:48

除非您有一个带有 ValuePair的非通用基本类型 ValuePair 。 :ValuePair(它也适用于接口),或使用List。但实际上,这是合理的:

public abstract class ValuePair
{
    public string Name { get; set; }
    public object Value
    {
        get { return GetValue(); }
        set { SetValue(value); }
    }
    protected abstract object GetValue();
    protected abstract void SetValue(object value);
}
public class ValuePair<T> : ValuePair
{
    protected override object GetValue() { return Value; }
    protected override void SetValue(object value) { Value = (T)value; }
    public new T Value { get; set; }
}

Not unless you have a non-generic base-type ValuePair with ValuePair<T> : ValuePair (it would work for an interface too), or use List<object>. Actually, though, this works reasonably:

public abstract class ValuePair
{
    public string Name { get; set; }
    public object Value
    {
        get { return GetValue(); }
        set { SetValue(value); }
    }
    protected abstract object GetValue();
    protected abstract void SetValue(object value);
}
public class ValuePair<T> : ValuePair
{
    protected override object GetValue() { return Value; }
    protected override void SetValue(object value) { Value = (T)value; }
    public new T Value { get; set; }
}
新人笑 2024-12-07 05:57:48

不,这是不可能的。根据您的情况,您可以创建一个派生自 ValuePair 的基类 ValuePair。取决于您的目的。

No, it is not possible. You could create, in your case, a base class ValuePair from which ValuePair<T> derives. Depends on your purposes.

似最初 2024-12-07 05:57:48

据我所知这是不可能的。

该行:

List<ValuePair> list = new List<ValuePair>();

您在示例中编写的内容没有为 T 提供具体类型,这就是问题所在,一旦通过它,您只能添加该特定类型的对象。

it's not possible as far as I know.

the line:

List<ValuePair> list = new List<ValuePair>();

you wrote in your sample is not providing a concrete type for T and this is the issue, once you pass it, you can only add object of that specific type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文