I have a base abstract class that also implements a particular interface.
public interface IMovable<TEntity, T>
where TEntity: class
where T: struct
{
TEntity Move(IMover<T> moverProvider);
}
public abstract class Animal : IMovable<Animal, int>
{
...
public virtual Animal Move(IMover<int> moverProvider)
{
// performs movement using provided mover
}
}
Then I have inherited classes some of which have to override interface implementation methods of the base class.
public class Snake : Animal
{
...
public override Animal Move(IMover<int> moverProvider)
{
// perform different movement
}
}
My interface methods return the same object instance after it's moved so I can use chaining or do something directly in return
statement without using additional variables.
// I don't want this if methods would be void typed
var s = GetMySnake();
s.Move(provider);
return s;
// I don't want this either if at all possible
return (Snake)GetMySnake().Move(provider);
// I simply want this
return GetMySnake().Move(provider);
As you can see in my example my overrides in child class returns base class type instead of running class. This may require me to cast results, which I'd like to avoid.
How can I define my interface and implementations so that my overrides will return the actual type of the executing instance?
public Snake Move(IMover<int> moverProvider) {}
I suggest changing the return type of the interface method to void
and moving the chaining behaviour to an extension method where you can get the real type of the target e.g.
public interface IMovable<TEntity, T>
where TEntity : class
where T : struct
{
void MoveTo(IMover<T> moverProvider);
}
public abstract class Animal : IMovable<Animal, int>
{
public virtual void MoveTo(IMover<int> mover) { }
}
public static class AnimalExtensions
{
public static TAnimal Move<TAnimal>(this TAnimal animal, IMover<int> mover) where TAnimal : Animal, IMovable<TAnimal, int>
{
animal.MoveTo(mover);
return animal;
}
}
Note you can make the Move
extension more generic if you need it to apply more generally:
public static TEntity Move<TEntity, T>(this TEntity entity, IMover<T> mover) where TEntity : IMovable<TEntity, T> where T : struct
{
entity.MoveTo(mover);
return entity;
}
You can convert Animal
to a generic type that accepts the concrete type as a type parameter:
public abstract class Animal<T> : IMovable<T, int> where T:Animal<T>
{
public virtual T Move(IMover<int> moverProvider)
{
...
}
}
public class Snake : Animal<Snake>
{
public override Snake Move(IMover<int> moverProvider)
{
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With