【问题标题】:C# child class returning a unknown type (futher: from parent)C# 子类返回未知类型(进一步:来自父类)
【发布时间】:2010-11-04 14:09:35
【问题描述】:

我有接口:

public interface Inx<T>
{
   T Set(Data data);
}

使用此方法的简单类

public class Base
{
   ??? Set(Data data) { ... }
}

和这样的父类:

public class Parent : Base, Inx<Parent>
{
   ...
}

我想从子类中的 Set 方法返回父类型 这是可能的 ? 我需要它来做这样的事情:

list.Add(new Parent().Set(data));

现在我必须这样做:

T t = new T();
t.Set(data);
list.Add(t);

它有点烦人,我必须使用它很多次


很抱歉,我可以使用这样的垃圾邮件:

this.GetType().GetConstructor(new System.Type[] { typeof(Data) }).Invoke(new object[] { data })

所以也许好的解决方案是从此方法返回一个对象;\ ?

具有泛型接口的泛型类似乎会浪费大量内存...因为此类的功能相同,只是返回类型不同

【问题讨论】:

    标签: c# types return parent


    【解决方案1】:

    如果我使用通用基类,我不能将父类转换为一种类型, 我不知道为什么我在那里尝试使用泛型...我现在找到了解决方案:)

    谢谢帮助

    interface Inx
    {
       object Set(Data data);
    }
    class Base
    {
       object Set(Data data)
       {
          // ...
          return (object) this;
       }
    }
    class Parent: Base, Inx
    {
       // i don't have too write here Set (im using from base one)
    }
    class ManageParent<T> where T: Inx, new()
    {
       // ...
          list.Add((T) new T().Set(data));
       // ...
    }
    

    【讨论】:

      【解决方案2】:

      这是你想要的吗?

      interface Inx<T> { 
          T Set(Data data);
      }
      public class Base
      {
          public virtual T Set<T>(Data data)
          {
              T t = default(T);
              return t;
          }
      }
      public class Parent : Base, Inx<Parent>
      {
          public Parent Set(Data data)
          {
              return base.Set<Parent>(data);
          }
      }
      class Program
      {
          static void Main(string[] args)
          {
              var data = new Data();
              var list = new List<Parent>();
              list.Add(new Parent().Set<Parent>(data));
              // or 
              list.Add(new Parent().Set(data));
          }
      }
      

      编辑:正如 Marc 所说,最好将接口实现移至 Base 类:

      interface Inx<T> { 
          T Set(Data data);
      }
      public class Base<T> : Inx<T>
      {
          public virtual T Set(Data data)
          {
              T t = default(T);
              return t;
          }
      }
      public class Parent : Base<Parent>
      {        
      }
      class Program
      {
          static void Main(string[] args)
          {
              var data = new Data();
              var list = new List<Parent>();
              list.Add(new Parent().Set(data));            
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以做到这一点的唯一方法是使Base 通用(Base&lt;T&gt;)并让Set 返回T - 然后拥有Parent : Base&lt;Parent&gt;。问题是...Set 怎么知道如何创建T?你可以有where T : new() 子句...

        这里的一个有用之处是您可以将接口实现移动到Base&lt;T&gt;

        public class Base<T> : Inx<T> where T : new()
        {
           public T Set(Data data) {
               T t = new T();
               ///
               return t;
           }
        }
        public class Parent : Base<Parent> { }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-11
          • 2022-11-17
          • 1970-01-01
          • 2016-10-28
          • 1970-01-01
          • 2014-04-01
          • 1970-01-01
          相关资源
          最近更新 更多