更改继承属性的类型(一个继承型) [英] Changing the Type of a inherited property (to a inherited type)
问题描述
我有除其他元信息,其中包含一个有向图的根节点的类。让我们称之为容器类。此容器可以出现在两种不同的模式,编辑模式和配置模式。根据不同的模式,根节点是一个不同类型的 NodeEdit 或 NodeConfig ,从相同的子类继承两者
using C# I have a class which contains among other meta information the root node of a directed graph. Let's call this the Container-Class. This container can appear in two different modes, Editor-Mode and Configurator-Mode. Depending on the mode, the root-node is of a different type NodeEdit or NodeConfig, both inheriting from the same subclass.
public abstract class NodeBase
{
string Name { get; set; }
...
}
public class NodeEdit : NodeBase ...
public class NodeConfig : NodeBase ...
有关容器中,我也创建一个基类,从它继承:
For the container, I also create a base class and inherit from it:
public abstract class ContainerBase
{
NodeBase Root { get; set; }
...
}
在由ContainerBase继承创建编辑器 - 和Configuratorcontainer类,我想成为根的类型 - 特定的(从NodeBase继承)属性类型,如:
When creating the classes for Editor- and Configuratorcontainer by inheriting from ContainerBase, I want to become the type of the Root - property the specific (inherited from NodeBase) type like:
public class ContainerEditor : ContainerBase
{
NodeEditor Root { get; set; }
...
}
但我不能改变ContainerBase定义的属性的类型。有没有办法解决这个问题的方法吗?我可以使用BaseNode型,并添加NodeEditor的元素像
But I cannot change the type of a property defined in ContainerBase. Is there a way to solve this problem? I can use the BaseNode-type, and add an element of NodeEditor like
ContainerEditorInstance.Root = new NodeEditor();
因为类型NodeEditor从类型BaseEditor继承,但在容器Editor类,我想明确地只允许根属性的类型为 NodeEditor 。
我可以在二传手检查并拒绝所有的节点,但这些类型的NodeEditor,但我想有属性是特定类型的,这样我就可以在编译时错误的检测任务。
because the type NodeEditor is inherited from type BaseEditor, but in the Container-Editor class, I want to explicitly only allow the type of the Root-property to be NodeEditor. I could check this in the setter and reject all nodes but those of type NodeEditor, but I'd like to have the property be of the specific type, so I can detect wrong assignments at compile-time.
在前进,结果谢谢
弗兰克
Thanks in advance,
Frank
推荐答案
您可以重新定义它:
public class ContainerEditor : ContainerBase
{
public NodeEditor Root {
get { return (NodeEditor)base.Root; }
set { base.Root = value; }
}
...
}
这篇关于更改继承属性的类型(一个继承型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!