【问题标题】:Collection of Generic Interfaces通用接口集合
【发布时间】:2018-12-22 04:16:50
【问题描述】:

我有一个这样的通用接口:

public interface IHardwareProperty<T>{
bool Read();
bool Write();
}

这是“通过”一个抽象基类:

public abstract class HardwareProperty<T>:IHardwareProperty<T>{
//Paritally implements IHardwareProperty (NOT SHOWN), and passes Read and Write through
//as abstract members.
    public abstract bool Read();
    public abstract bool Write();
}

并在几个使用不同泛型参数的继承类中完成

CompletePropertyA:IHardwareProperty<int>
{
    //Implements Read() & Write() here
}

CompletePropertyBL:IHardwareProperty<bool>
{
    //Implements Read() & Write() here
}

我想在同一个集合中存储一堆不同类型的完整属性。 有没有办法做到这一点而不求助于收集objects?

【问题讨论】:

  • 我在这里看到的完成您想要的唯一方法是添加一个非通用接口作为通用接口的基础,例如public interface IHardwareProperty&lt;T&gt; : IHardwareProperty 然后存储基数:List&lt;IHardwareProperty&gt; propertyList...
  • 为什么你的界面首先是通用的?你从不使用T...
  • 谢谢,我实际上是在发帖几分钟后才知道的。我想输入问题帮助我解决了问题。我改变了它,所以 IHardwareProperty 有一个 object GetValue() 方法。它不太好,因为当我通过它访问值时我仍然需要强制转换它,但它完成了工作。

标签: c# generics inheritance


【解决方案1】:

您需要使用所有这些都支持的类型。您可以通过将 IHardwareProperty&lt;T&gt; 接口设为非泛型来做到这一点:

public interface IHardwareProperty
{
    bool Read();
    bool Write();
}

由于您的界面中没有一个方法使用T,因此这是非常合适的。使接口泛型的唯一原因是您在接口方法或属性中使用泛型类型。

请注意,如果实现细节需要或需要,您的基类仍然可以是通用的:

public abstract class HardwareProperty<T> : IHardwareProperty
{
   //...

【讨论】:

    【解决方案2】:

    很遗憾不是,因为每个具有不同类型参数的泛型类型都被视为完全不同的类型。

    typeof(List<int>) != typeof(List<long>)
    

    【讨论】:

      【解决方案3】:

      为后代发布此内容:我的问题几乎与此相同,但稍作调整,我的界面确实使用了泛型类型。

      我有多个实现通用接口的类。目标是拥有多个 &lt;Int/Bool/DateTime&gt;Property 类,每个类都包含一个字符串 (_value) 和一个 getValue() 函数,该函数在调用时会将字符串 _value 转换为不同的类型,具体取决于接口的实现。

      public interface IProperty<T>
      {
          string _value { get; set; }
          T getValue();
      };
      
      public class BooleanProperty : IProperty<bool>
      {
          public string _value { get; set; }
          public bool getValue()
          {
              // Business logic for "yes" => true
              return false;
          }
      }
      
      public class DateTimeProperty : IProperty<DateTime>
      {
          public string _value { get; set; }
          public DateTime getValue()
          {
              // Business logic for "Jan 1 1900" => a DateTime object
              return DateTime.Now;
          }
      }
      

      然后我希望能够将这些对象中的多个添加到一个容器中,然后在每个容器上调用getValue(),它会返回一个布尔值、日期时间或其他取决于它的类型。

      我想我可以做到以下几点:

      List<IProperty> _myProperties = new List<IProperty>();
      

      但这会产生错误:

      Using the generic type 'IProperty<T>' requires 1 type arguments
      

      但我还不知道这个列表的类型是什么,所以我尝试添加一个类型&lt;object&gt;

      List<IProperty<object>> _myProperties = new List<IProperty<object>>();
      

      然后编译。然后我可以将项目添加到集合中,但我需要将它们转换为 IProperty&lt;object&gt;,这很难看,老实说,我不确定这是在做什么。

      BooleanProperty boolProp = new BooleanProperty();
      // boolProp.getValue() => returns a bool
      DateTimeProperty dateTimeProp = new DateTimeProperty();
      // dateTimeProp.getValue(); => returns a DateTime
      _myProperties.Add((IProperty<object>)boolProp);
      _myProperties.Add((IProperty<object>)dateTimeProp);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-27
        • 1970-01-01
        • 1970-01-01
        • 2012-03-07
        • 2014-10-17
        • 1970-01-01
        • 2013-01-23
        • 1970-01-01
        相关资源
        最近更新 更多