我有一个泛型集合路径,其中T是段 - 段是抽象类。 我有一个包含从抽象基类段经由中间类线段派生类SpecialLineSegments路径的派生类ClosedPath。 我想选择满足条件的路径,然后我将修改它,使得它可以包含不同类型段的,可能不是一个ClosedPath了....所以我试图转换为路径。编译器给出错误,表明这种转换是不可能的。如何转换为普通集合
public static void Method1(ClosedPath[] paths)
{
bool condition = false;
//working code..
Path<Segment> Pslct = new Path<Segment>();
foreach (ClosedPath P in paths)
{
if (condition)
{
//working code
Pslct = (Path<Segment>) P;
}
}
}
路径定义如下......
public class Path<T> : IEnumerable<T> where T : Segment
{
private List<T> segments = new List<T>();
public List<T> Segments
{
set { segments = value;}
get { return this.segments; }
}
public T this[int pos]
{
get { return (T)segments[pos]; }
set { segments[pos] = value; }
}
public Path()
{
this.Segments = new List<T>();
}
public Path(List<T> s)
{
this.Segments = s;
}
public void AddSegment(T s) {Segments.Add(s);}
public int Count {get {return Segments.Count;}}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{ return Segments.GetEnumerator();}
IEnumerator IEnumerable.GetEnumerator()
{ return Segments.GetEnumerator(); }
}
ClosedPath从
public class LinePath<T> : Path<T>, IEnumerable<T> where T : LineSegment
{
//working code
}
线段源自段
如何定义路径? – 2012-03-19 14:00:50
@James上面编辑 – gwizardry 2012-03-19 14:10:04
写一个显式的转换操作符。 – phoog 2012-03-19 14:10:34