gwi*_*dry 1 c# generics casting explicit-conversion
我试图在consturctor中为一个派生类IntersectionPath转换一个对象列表,如下所示.
public class IntersectionPath : Path<IntersectionSegment>, IEnumerable
{
//Constructors
public IntersectionPath() : base() { Verts = null; }
public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base()
{
this.Segments = (List<IntersectionSegment>) inEdges;
}
}
Run Code Online (Sandbox Code Playgroud)
段在通用基类Path中定义
public class Path<T> : IEnumerable<T> where T : Segment<Node>
{
//public properties
public List<Direction> Directions {get; set; }
public List<T> Segments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经为IntersectionSegment类中的强制转换定义了一个显式运算符(见下文,因此不清楚为什么这不会编译.我在IntersectionPath构造函数中有一个错误消息.
public class IntersectionSegment : Segment<Intersection>
{
//curves which intersect the primary curve at I0(Start Node) and I1(End Node)
public Curve C0 { get; set; }
public Curve C1 { get; set; }
public IntersectionSegment():base() {}
public IntersectionSegment(Intersection n0, Intersection n1):base(n0,n1){}
public static explicit operator IntersectionSegment(Segment<Node> s)
{
if ((s.Start is Intersection) && (s.End is Intersection))
{
return new IntersectionSegment(s.Start as Intersection,s.End as Intersection);
}
else return null;
}
public static explicit operator List<IntersectionSegment>(List<Segment<Node>> ls)
{
List<IntersectionSegment> lsout = new List<IntersectionSegment>();
foreach (Segment<Node> s in ls)
{
if ((s.Start is Intersection) && (s.End is Intersection))
{
lsout.Add(new IntersectionSegment(s.Start as Intersection,s.End as Intersection));
}
else return null;
}
return lsout;
}
Run Code Online (Sandbox Code Playgroud)
细分定义为:
public class Segment <T> : Shape where T : Node
{
//generic properties
public T Start { get; set; }
public T End { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
List<InteractionSegment>
是不一样的InteractionSegment
.将一种类型的列表强制转换为另一种类型的列表将不会强制转换每个项目.
你需要做这样的事情:
this.Segments = inEdges.Select(x => (InteractionSegment)x).ToList();
Run Code Online (Sandbox Code Playgroud)
这使用LINQ to Objects将每个对象强制转换inEdges
为InteractionSegment
对象,并将结果放回到随后分配给的列表中this.Segments
.
让我们看一个不那么令人困惑的例子。
class Animal {}
class Giraffe : Animal {}
class Tiger : Animal {}
...
List<Giraffe> giraffes = new List<Giraffe>();
List<Animal> animals = (List<Animal>) giraffes; // illegal
Run Code Online (Sandbox Code Playgroud)
你的问题是我相信“为什么最后一行的演员是非法的?”
让我们假设这是合法的。现在我们添加另一行:
animals.Add(new Tiger());
Run Code Online (Sandbox Code Playgroud)
您可以将老虎添加到动物列表中,对吗?但那份动物清单实际上是一份长颈鹿清单。演员表不复制列表,它说“我想将此对象视为这种类型”。但是因为这样做会让你做一些疯狂的事情,比如把一只老虎放进长颈鹿的名单,我们把这个演员定为非法。
您的情况只是相同情况的复杂得多的版本。
这个问题几乎每天都会在 StackOverflow 上被问到。寻找“协方差和逆变”,你会发现几十个例子。
归档时间: |
|
查看次数: |
2562 次 |
最近记录: |