Reputation: 1524
I am trying to setup my waypoint system in a more flexible way so i can use derived classes for more customisation but i am stuck with how to set the ability to define the derived type to it.
I have the interface like this:
public interface ISegment
{
IWaypoint WaypointA { get; set; }
IWaypoint WaypointB { get; set; }
}
And my Segment has:
public class Segment : ISegment
{
private Waypoint _waypointA;
public IWaypoint WaypointA
{
get => _waypointA;
set => _waypointA = value;
}
private Waypoint _waypointB;
public IWaypoint WaypointB
{
get => _waypointB;
set => _waypointB = value;
}
}
Waypoint Class:
public class Waypoint : IWaypoint
{
public Vector3 Position {get; set;} // from IWaypoint
//...custom logic ... //
}
Where Waypoint
is my derived class with all my custom logic. But i can't do it this way because it won't convert the IWaypoint
to the backing field _waypoint
of type Waypoint : IWaypoint
.
What is the correct way to set this up so i can apply my own custom waypoint class but still have the rigid contract setup that an interface demands?
Upvotes: 1
Views: 69
Reputation: 56162
Probably you can implement ISegment
explicitly, i.e.:
public class Segment : ISegment
{
public Waypoint WaypointA { get; set; }
IWaypoint ISegment.WaypointA
{
get => WaypointA;
set => WaypointA = (Waypoint) value; // throw an exception if value is not of type Waypoint
}
}
public interface IWaypoint { }
public class Waypoint : IWaypoint { }
public interface ISegment
{
IWaypoint WaypointA { get; set; }
}
Upvotes: 1