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 を使用して各オブジェクトをオブジェクトにキャストしinEdgesInteractionSegment結果をリストに戻してから に割り当て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

List<Segment<Node>>a が aではないという理由だけで機能しませんList<IntersectionSegment>。後で作成する場合Cast()は、リスト内の各項目を必要なタイプに明示的にキャストするために使用できます。

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