在派生类C#中扩展嵌套类

时间:2014-09-15 07:12:17

标签: c# oop

我希望在其中有一个带有嵌套类的基类 - 并展开子类中的嵌套类: 出于某种原因 - 我无法访问嵌套派生类NEW(已添加)属性:

public class BaseClass
{
    public NestedClass nestedClass {get; set;}
    public BaseClass()
    {
       nestedClass = new NestedClass();
    }
    public int baseproperty {get; set;}
    public  class NestedClass
    {
        public int NestedProperty {get; set;}
    }
}

public class Derived : BaseClass
{
    public class NestedClass
    {
          public DeeperNestedClass deeper {get; set;}
          public NestedClass()
          {
              deeper = new DeeperNestedClass();
          }
          public class DeeperNestedClass
          {
               public int VeryDeepPropery {get; set;}
          }


    }
}

Derived MyDerived = new Derived();
MyDerived.nestedClass.deeper   <----- Doesn't recognized !

* 编辑 - 修复了基类中缺少的“类”

3 个答案:

答案 0 :(得分:3)

首先,您必须在声明期间指定NestedClassclasspartial

public partial class NestedClass  // You forgot those two here...
{
    public int NestedProperty { get; set; }
}

此外,您不能将嵌套类拆分为两个单独的类,即使其中一个派生自另一个类。您只能在原始基类中进行拆分并将其标记为部分基类:http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.120).aspx

嗯..至少不是一个理智的方式。 cremor似乎找到了解决办法,但我会避开这种代码。

答案 1 :(得分:3)

你基本上有三个问题:

1)您实际上必须从Derived.NestedClass派生BaseClass.NestedClass

2)您的属性BaseClass.nestedClass将始终具有BaseClass.NestedClass类型,因此您需要将其强制转换为deeper属性。

3)在BaseClass.nestedClass中访问时,您必须确保属性Derived.NestedClass实际上包含Derived类型的对象。

public class BaseClass {
   public NestedClass nestedClass { get; set; }

   public BaseClass() {
      nestedClass = new NestedClass();
   }

   public int baseproperty { get; set; }

   public class NestedClass {
      public int NestedProperty { get; set; }
   }
}

public class Derived : BaseClass {
   public Derived() {
      nestedClass = new NestedClass();
   }

   public class NestedClass : BaseClass.NestedClass {
      public DeeperNestedClass deeper { get; set; }

      public NestedClass() {
         deeper = new DeeperNestedClass();
      }

      public class DeeperNestedClass {
         public int VeryDeepPropery { get; set; }
      }
   }
}

class Program {
   static void Main(string[] args) {
      Derived derived = new Derived();
      BaseClass.NestedClass nestedBase = derived.nestedClass;
      Derived.NestedClass nestedDerived = (Derived.NestedClass)nestedBase;
      Derived.NestedClass.DeeperNestedClass deeper = nestedDerived.deeper;
      int property = deeper.VeryDeepPropery;
   }
}

在评论中讨论后编辑: 也许这样简单的东西对你有用吗?

public class OuterClass {
   public OuterClass() {
      nestedClass = new NestedClass();
   }

   public int baseproperty { get; set; }
   public NestedClass nestedClass { get; set; }
}

public class NestedClass {
   public NestedClass() {
      deeper = new DeeperNestedClass();
   }
   public int NestedProperty { get; set; }
   public DeeperNestedClass deeper { get; set; }
}

public class DeeperNestedClass {
   public int VeryDeepPropery { get; set; }
}

class Program {
   static void Main(string[] args) {
      OuterClass outer = new OuterClass();
      int property = outer.nestedClass.deeper.VeryDeepPropery;
   }
}

答案 2 :(得分:2)

您不能将嵌套在不同类中的部分类。

否则,它的全名是什么?

Namespace.BaseClass.NestedClassNamespace.Derived.NestedClass?对于不同的名称引用同一个类也没有意义。

出于同样的原因,你不能在不同的命名空间中拥有它。

可能的解决方案是创建一个接口:

interface INested { }

让你的BaseClass通用:

class BaseClass<T> where T : INested, new()
{
   public T Nested { get; set; }
   public BaseClass()
   {
       Nested = new T();
   }
}

class Derived : BaseClass<NestedImpl>
{
   Derived()
   {
       Nested = new NestedImpl();
   }

   public class NestedImpl : INested
   {
       public int deeper {get;set;}
   }
}