I have quite simple class hierarchy:
public class Base
{
//...
public virtual void AssignFrom(Base baseObj)
{
//DoSomeStuff
}
}
public class DerivedA : Base
{
//...
public override void AssignFrom(Base derivedObj)
//Wish Method would be:
//public override void AssignFrom(DerivedA derivedObj)
{
base.AssignFrom(derivedObj);
var objAsDerivedA = derivedObj as DerivedA;
//Some other stuff concerning derived class
}
}
public class DerivedB : Base
{
//...
public override void AssignFrom(Base derivedObj)
//Wish Method would be:
//public override void AssignFrom(DerivedB derivedObj)
{
base.AssignFrom(derivedObj);
//Here I have to cast, what I would gladly avoid.
var objAsDerivedB = derivedObj as DerivedB;
//Some other stuff concerning derived class
}
}
What bothers me is that each sibling of derived class or it's descendant can be used as parameter.
It would be possible to create own, non virtual method for each class, but in my opinion the association/connection between methods will be lost.
Question: Is there a way to restrict method's parameter to the class, where the method is overridden.