, . - , , , 4 .
, .
1: .
public class ItemParent {
public List<ItemChild> MyChildren {get; set;}
}
public class ItemChild {
public ItemParent MyParent {get; set;}
}
public class Car : ItemParent {
public List<CarPart> MyParts {get; set;}
}
public class CarPart : ItemChild {
public Car ParentCar {get; set;}
}
, CarPart, ItemChild. , .
public class ItemParent<T> where T : ItemChild {
public List<T> MyChildren {get; set;}
}
public class ItemChild<T> where T : ItemParent {
public T MyParent {get; set;}
}
public class Car : ItemParent<CarPart> {}
public class CarPart : ItemChild<Car> {}
public class Truck : ItemParent<TruckPart> {}
public class TruckPart : ItemChild<Truck> {}
subclass.MyChildren [] MyParts, MyChildren.
, - , / . , Truck-TruckParts ( -, - ..), .
/ "" (, ), :
public class ParentChildCollection<TParent, TChild> {}
public class Car {
private ParentChildCollection<Car, CarPart> PartHierarchy;
public List<CarPart> MyParts {get { return PartHierarchy.GetMyChildren(this); } }
}
public class CarPart {
private ParentChildCollection<Car, CarPart> PartHierarcy;
public Car ParentCar {get { return PartHierarchy.GetMyParent(this); }}
}
, , Truck Car ( , ).
2. , .
public class Car {
public List<CarPart> MyParts {get; set;}
}
public class CarPart {
public Car MyParent {get; set;}
}
public class Truck : Car {
public List<TruckPart> MyParts {get; set;}
}
public class TruckPart : CarPart {
public Truck MyParent {get; set;}
}
Truck Car . , . (Vehicle-VehiclePart). . / stictly Car Truck.
, , . , - , ( ) .