I think maybe you are looking for a constructor template. Each level defines a constructor protectedthat copies the corresponding properties:
public class Parent
{
public string Name { get; set; }
public string City { get; set; }
public Parent()
{
}
protected Parent(Parent copy)
{
this.Name = copy.Name;
this.City = copy.City;
}
}
Child Parent, -, :
public class Child : Parent
{
public string NewInfo { get; set; }
public Child(Parent copy)
: base(copy)
{
}
}
:
Parent parent = new Parent() { Name = "Name", City = "StackOverflow"};
Child child = new Child(parent) { NewInfo = "Something new!" };
Console.WriteLine(child.Name);
Console.WriteLine(child.City);
Console.WriteLine(child.NewInfo);
, , .
EDIT: :
- , , , .
, :
public class Child
{
private readonly Parent WrappedParent;
public string NewInfo { get; set; }
public string Name
{
get { return WrappedParent.Name; }
set { WrappedParent.Name = value; }
}
public string City
{
get { return WrappedParent.City; }
set { WrappedParent.City = value; }
}
public Child(Parent wrappedParent)
{
this.WrappedParent = wrappedParent;
}
}
- , ( a) "Parent", " " . "" IParent, , - "" , IParent .