在派生类C#中展开嵌套类
本文关键字:嵌套 派生 | 更新日期: 2023-09-27 18:21:49
我想有一个基类,里面有一个嵌套类,并在子类中展开嵌套类:由于某些原因-我无法访问嵌套派生类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 !
*编辑-修复了基类中丢失的"class"
您基本上有三个问题:
1) 实际上,您必须从BaseClass.NestedClass
派生Derived.NestedClass
。
2) 您的属性BaseClass.nestedClass
将始终具有BaseClass.NestedClass
的类型,因此您需要强制转换它才能访问deeper
属性。
3) 当在Derived
中访问时,必须确保属性BaseClass.nestedClass
实际包含类型为Derived.NestedClass
的对象。
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;
}
}
首先,您必须在声明期间指定NestedClass
是class
和partial
:
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
似乎已经找到了解决方案,但我会避开这种代码。
不能在不同的类中嵌套分部类。
否则,它的全名是什么?
Namespace.BaseClass.NestedClass
还是Namespace.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;}
}
}