【问题标题】:Extending nested class which is using as property扩展用作属性的嵌套类
【发布时间】:2011-07-16 10:26:31
【问题描述】:

我编写了一个嵌套类,用作属性的包。此类用作我命名为Properties 的属性。我想通过接口扩展属性的数量。

我写了这个例子:

public interface IFirst {
    int asd { get; set; }
}

public interface ISecond {
    int zxc { get; set; }
}

public class MyClass {
    public class PropertyClass : IFirst {
        public int asd {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }
    }

    public PropertyClass Properties; 
}

public class MyNextClass : MyClass {
    public class PropertyClass : MyClass.PropertyClass, ISecond {
        public int zxc {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }
    }

    public void test() {
        Properties.zxc = 5; // Here is problem
    }
}

但在这种情况下,我无法读取/写入新属性 zxc

我认为因为这仍然是从父类读取 Properties 类型 - MyClass.PropertyClass 而不是 MyNextClass.PropertyClass

我想在不创建新属性或隐藏现有属性的情况下扩展它。

你有什么建议吗?

【问题讨论】:

    标签: c# inheritance interface properties nested-class


    【解决方案1】:

    您必须确保父类实现这两个接口,或者您必须在嵌套子类型的子类中创建一个新的静态成员。正如您所猜想的那样,Properties 被声明为父嵌套类型,并且在从父嵌套类型派生的同名子类中声明一个新类型不会改变这一点。

    【讨论】:

      【解决方案2】:

      嗯,根据您要达到的目标,方法可能会有所不同。例如,使用抽象类可能满足您的需求。像这样:

      public interface IFirst
      {
          int asd { get; set; }
      }
      
      public interface ISecond
      {
          int zxc { get; set; }
      }
      
      public abstract class MyAbstractClass<T> where T : class
      {
          public abstract T Properties {get; set;}
      }
      
      public class MyClass : MyAbstractClass<MyClass.PropertyClass>
      {
          public class PropertyClass : IFirst
          {
              public int asd
              {
                  get { throw new NotImplementedException(); }
                  set { throw new NotImplementedException(); }
              }
          }
      
          public override MyClass.PropertyClass Properties
          {
              get { throw new NotImplementedException(); }
              set { throw new NotImplementedException(); }
          }
      }
      
      public class MyNextClass : MyAbstractClass<MyNextClass.PropertyClass>
      {
          public class PropertyClass : MyClass.PropertyClass, ISecond
          {
              public int zxc
              {
                  get { throw new NotImplementedException(); }
                  set { throw new NotImplementedException(); }
              }
          }
      
          public override MyNextClass.PropertyClass Properties
          {
              get { throw new NotImplementedException(); }
              set { throw new NotImplementedException(); }
          }
      
          public void test()
          {
              Properties.zxc = 5;
          }
      }
      

      【讨论】:

      • 这行得通,但也打破了从MyClassMyNextClass 的继承链。话虽这么说,但不知道更多,那很可能没问题。
      • @dlev 是的,我知道这不是@nosbor 所要求的……但正如你所说,在不了解更多信息的情况下,我们所能做的就是为可能的问题提出可能的解决方案:)
      • Bad new...当我回到家并尝试使用您的解决方案时出现错误:错误 1 ​​'MyNextClass' 没有实现继承的抽象成员 'MyAbstractClass.Properties.set '
      • 不,它应该可以工作。给我一秒钟,我会试着找出问题。
      • @nosbor 完成(修复了源代码)。我在MyClass 中实现了属性,但在MyNextClass 中忘记了这样做。它现在编译:)
      猜你喜欢
      • 2020-02-05
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 2016-06-10
      • 2023-01-10
      相关资源
      最近更新 更多