本文介绍了通用接口列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一对夫妇的实现类如通用接口:

 公共接口IDataElement< T> 
{
INT DataElement {搞定;组; }
的T值{搞定;组; }
}

公共类IntegerDataElement:IDataElement< INT>
{
公众诠释DataElement {搞定;组; }
公共int值{搞定;组; }
}

公共类StringDataElement:IDataElement<串GT;
{
公众诠释DataElement {搞定;组; }
公共字符串值{搞定;组; }
}

是否有可能通过不同类型的实现类的集合,而不必求助于传递作为对象



它似乎没有可以定义一个返回值作为

 公共IDataElement< T> []的GetData()

对象> []的GetData()

 公共IDataElement< $ p> 

任何建议


解决方案

您当然可以声明:

 公共IDataElement< T> []的GetData< T>()

 公共IDataElement<对象> []的GetData()




  • 尽管后者可能不是你后(你的接口也不会变形,即使在C#4,因为它使用 T 在一个输入和输出的位置;即使它是变种,你就不能使用该方差值类型)。前者需要调用者指定< T> ,如:



    foo的。的GetData<串>();




这样行不行你。

有没有表达方式对象的集合,其中每个工具 IDataElement<的; T> 为不同的T,除非你也给它一个非泛型基类,你可以只使用的IList<在其中; IDataElement> 。在这种情况下,非泛型 IDataElement 可以有 DataElement 属性,使中的泛型接口属性:

 公共接口IDataElement 
{
INT DataElement {得到;组; }
}

公共接口IDataElement< T> :IDataElement
{
的T值{搞定;组; }
}

是有用的在您的特定情况呢?



目前尚不清楚你要如何使用数据元素的集合,而不需要知道它们的类型......如果上述不帮助你,也许你会说更多关于你的期望做什么与收藏。


If I have a generic interface with a couple of implementing classes such as:

public interface IDataElement<T>
{
    int DataElement { get; set; }
    T Value { get; set; }
}

public class IntegerDataElement : IDataElement<int>
{
    public int DataElement { get; set; }
    public int Value { get; set; }
}

public class StringDataElement : IDataElement<String>
{
    public int DataElement { get; set; }
    public String Value { get; set; }
}

Is it possible to pass a collection of the implementing classes of differing types, without having to resort to passing as object.

It does not appear to be possible to define a return values as

public IDataElement<T>[] GetData()

or

public IDataElement<object>[] GetData()

Any suggestions?

解决方案

You can certainly declare:

public IDataElement<T>[] GetData<T>()

and

public IDataElement<object>[] GetData()

  • although the latter probably isn't what you're after (your interface won't be variant even in C# 4 as it uses T in both an input and an output position; even if it were variant, you wouldn't be able to use that variance for value types). The former will require the caller to specify <T>, e.g.

    foo.GetData<string>();

Is that okay for you?

There's no way of expressing "a collection of object, each of which implements IDataElement<T> for a different T" unless you also give it a nongeneric base class, at which you could just use IList<IDataElement>. In this case the nongeneric IDataElement could have the DataElement property, leaving the Value property in the generic interface:

public interface IDataElement
{
    int DataElement { get; set; }
}

public interface IDataElement<T> : IDataElement
{
    T Value { get; set; }
}

Is that useful in your particular situation?

It's not clear how you'd want to use a collection of data elements without knowing their types... if the above doesn't help you, maybe you could say more about what you expected to do with the collections.

这篇关于通用接口列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:34