1

我正在尝试在派生类 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;
        }

    }

段在通用基类 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; }
    }

我已经为 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;
    }

段定义为:

public class Segment <T> : Shape where T : Node
{
    //generic properties
    public T Start { get; set; }
    public T End { get; set; }

 }
4

3 回答 3

7

List<InteractionSegment>不一样InteractionSegment。将一种类型的列表转换为另一种类型的列表不会转换每个项目。
你需要做这样的事情:

this.Segments = inEdges.Select(x => (InteractionSegment)x).ToList();

这使用 LINQ to Objects 将每个对象inEdges转换为一个InteractionSegment对象,并将结果放回一个列表中,然后分配给this.Segments.

于 2012-03-27T14:23:57.157 回答
5

让我们看一个不那么容易混淆的例子。

class Animal {}
class Giraffe : Animal {}
class Tiger : Animal {}
...
List<Giraffe> giraffes = new List<Giraffe>();
List<Animal> animals = (List<Animal>) giraffes; // illegal

你的问题是我相信“为什么最后一行的演员是非法的?”

让我们假设它是合法的。现在我们添加另一行:

animals.Add(new Tiger());

您可以将老虎添加到动物列表中,对吗?但那份动物名单实际上是一份长颈鹿名单。演员表不复制列表,它说“我想将此对象视为这种类型”。但是因为这样做会让你做一些疯狂的事情,比如把一只老虎放到长颈鹿的名单中,所以我们把演员表定为非法。

您的情况只是相同情况的更复杂的版本。

这个问题几乎每天都会在 StackOverflow 上被问到。寻找“协变和逆变”,你会发现几十个例子。

于 2012-03-27T14:45:07.053 回答
2

它不起作用仅仅因为 aList<Segment<Node>>不是 a List<IntersectionSegment>。如果您想创建后者,您可以使用Cast()将列表中的每个项目显式转换为您想要的类型:

this.Segments = inEdges.Cast<IntersectionSegment>().ToList();
于 2012-03-27T14:25:28.597 回答