クラスの派生クラス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;
}
}
セグメントが
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; }
}
あなたは*たくさんのコードを提供しています - それを1つの短い完全なプログラムに減らすことはできますか? –
私はそれを実現します....私はちょうど前の投稿で見つかった私はより多くのコードを求められる! :-s – gwizardry
*完全な*コードを与えていないのであれば、一般的にもっと尋ねられるでしょうが、短いものと完全なものは通常大丈夫です。 –