Calling a method on a generic type list from an abstract parent class

Here is my current type hierarchy:

enter image description here

I am trying to implement a method in PlaneRegion that will call a method called Shift() in a list in its derived classes, where the list is called PlaneBoundaries in all of them, but they are of different types

So:

 public abstract class PlaneRegion<T> { public abstract List<T> PlaneBoundaries { get; set; } } public class Polygon : PlaneRegion<LineSegment> { public override List<LineSegment> PlaneBoundaries { get { return _planeBoundaries; } set { _planeBoundaries = value; } } protected List<LineSegment> _planeBoundaries; } public class NonPolygon : PlaneRegion<IEdge> { public override List<IEdge> PlaneBoundaries { get { return _planeBoundaries; } set { _planeBoundaries = value; } } private List<IEdge> _planeBoundaries; } 

Ideally, it should also return a copy of the object as its subclass and not modify the original object.

I currently have an IEdge interface implemented by two classes: LineSegment and Arc. I use Generics for the abstract superclass PlaneRegion, because the two inheriting classes: Polygons and NonPolygon have planar borders, but Polygon contains only straight lines (lineSegments), while NonPolygon can have straight or curved lines (LineSegment or Arc), so I implemented as in this question, as you can see in the following snippets: Override a property with a derived type and the same name C #

However, since PlaneRegion and PlaneBoundaries in PlaneRegion are a typical type, this causes problems when I try to call shift on PlaneBoundaries. The following are examples of the implementation of Shift:

 //In PlaneRegion public PlaneRegion<T> Shift(Shift inShift) { //does not work because Shift isn't defined for type T this.PlaneBoundaries.Shift(passedShift); } //in Polygon public override Polygon Shift(Shift passedShift) { return new Polygon(this.PlaneBoundaries.Shift(passedShift)); } //In NonPolygon public override NonPolygon Shift(Shift passedShift) { return new NonPolygon(this.PlaneBoundaries.Shift(passedShift)); } 

Is there a way to cause a shift in a generic list like this, or limit the capabilities of T to classes implementing IEdge at compile time? I tried to make Shift in PlaneRegion also universal, but that didn't work either.

Also, ideally, I want it to return copies of the source objects as a child, and change the PlaneBoundaries to them instead of the original PlaneBoundaries, but do not.

+2
source share
1 answer

You can narrow the PlaneRegion class to only allow IEdge interface IEdge in T:

 public abstract class PlaneRegion<T> where T : IEdge { public abstract List<T> PlaneBoundaries { get; set; } } 

In addition, in the Shift function, you can apply it to all elements of the list, and not to the entire list, so you should change it to:

 //In PlaneRegion public PlaneRegion<T> Shift(Shift inShift) { this.PlaneBoundaries.ForEach(x => x.Shift(passedShift)); } 
+1
source

All Articles