问题描述
我创建了一个基类(元素)和基表类(元素)作为泛型类。泛型列表类应该只能够包含类,这是类型的元素源自元素。而元级应该拥有一个ParentRoot属性,它应包含基本列表类(元素)!
I created a base class ("Element") and a base list class ("Elements") as generic class.The generic list class should only be able to contain classes, which are of Type "Element" of derived from "Element".The "Element" class should own a "ParentRoot" property, which should contain the base list class ("Elements")!
public class Element
{
public Elements<Element> ParentRoot { get; set; }
}
public class Elements<T> : List<T> where T : Element
{
}
现在我创建两个类,哪些是衍生自上述类两个列表类。但我没有上设置ParentRoot属性:
Now i create two classes and two list classes which are derived form the classes above. But i'm failing on setting the "ParentRoot" property:
public class Ceiling : Element
{
public Ceiling(Ceilings parent)
{
Parent = parent;
ParentRoot = parent;
}
public Ceilings Parent { get; set; }
}
public class Ceilings : Elements<Ceiling>
{
}
public class Wall : Element
{
public Wall(Walls parent)
{
Parent = parent;
ParentRoot = parent;
}
public Walls Parent { get; set; }
}
public class Walls : Elements<Wall>
{
}
我得到两个错误的:
I get two errors at:
ParentRoot = parent;
无法隐式转换类型天花板到元素无法隐式转换类型的墙,以元素
Cannot implicitly convert type "Ceilings" to "Elements"Cannot implicitly convert type "Walls" to "Elements"
是否有一个解决这个问题?
Is there a solution for this problem?
感谢您的帮助!
编辑:
好吧,我有更具体一点。我扩大℃的位$ C $:
OK, i have to be a bit more specific.I expanded the code a bit:
public class Room
{
public Room(Rooms parent)
{
Parent = parent;
}
public Rooms Parent { get; set; }
}
public class Rooms : List<Room>
{
}
public class Element
{
public Elements<Element> ParentRoot { get; set; }
public Rooms FindRoomsToElement()
{
Rooms rooms = new Rooms();
foreach (Room room in ParentRoot.Parent.Parent)
{
// Do stuff here
// if i rename the "ParentRoot" property to "Parent" and make it "virtual",
// and the other properties overwrite it with the "new" key, then this will
// get a null exception!
// i haven't testet it, but i think abstrakt will bring the same/similar result
// if i make the "ParentRoot" property IEnumerable, then there will no
// ParentRoot.Parent be available
}
return rooms;
}
}
public class Elements<T> : List<T> where T : Element
{
public Elements(Room parent)
{
Parent = parent;
}
public Room Parent { get; set; }
}
public class Ceiling : Element
{
public Ceiling(Ceilings parent)
{
Parent = parent;
//ParentRoot = parent;
}
public Ceilings Parent { get; set; }
}
public class Ceilings : Elements<Ceiling>
{
public Ceilings(Room parent) : base(parent)
{
}
}
public class Wall : Element
{
public Wall(Walls parent)
{
Parent = parent;
//ParentRoot = parent;
}
public Walls Parent { get; set; }
}
public class Walls : Elements<Wall>
{
public Walls(Room parent) : base(parent)
{
}
}
我希望这使得它更precise。
I hope this makes it more precise.
推荐答案
您是不允许的,因为如果你能做到这一点,你可以把一种错误的元素融入一个列表
。
You aren't allowed to do this because if you could, you could put the wrong kind of elements into a List
.
Elements<Ceilings> ceilings = someCeilings;
Elements<Element> parentRoot = ceilings; // Not allowed; imagine it is though.
Wall wall = someWall;
parentRoot.Add(wall); // Oops - we just added a wall to someCeilings!
如果你只把墙壁和/或天花板作序,你可以使用的IEnumerable&LT;组件&gt;
,而不是(这工作,因为 IEnumerable的&LT; T&GT;
是协):
If you can just treat the walls and/or ceilings as a sequence, you can do use IEnumerable<Element>
instead (which works because IEnumerable<T>
is "covariant"):
IEnumerable<Element> parentRoot = ceilings; // OK
这是可以的,因为的IEnumerable&LT;组件&gt;
有没有办法修改原始集合
This is OK because IEnumerable<Element>
has no way to modify the original collection.
这篇关于我要如何转换成一个类(这是从一个普通的&QUOT而得;基地&QUOT;类)的通用和QUOT;基地&QUOT;类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!