I currently have a derived class and a base class. How can I make the base class of the derived class equal to a base class that I have? Will a shallow copy work?
I came up with a pretty good pattern for dealing with this situation.
public class Base
{
public int BaseField;
///
/// Apply the state of the passed object to this object.
///
public virtual void ApplyState(Base obj)
{
BaseField = obj.BaseField;
}
}
public class Derived : Base
{
public int DerivedField;
public override void ApplyState(Base obj)
{
var src = srcObj as Derived;
if (src != null)
{
DerivedField = src.DerivedField;
}
base.ApplyState(srcObj);
}
}
Given any two objects that share type 'Base', you can apply A to B or B to A.