Just to get the ball rolling here's howThis works to make your class hierarchy strongly-typed & comply with the CreateSample code:
public abstract class AdjacencyTreeBase<T> where T : AdjacencyTreeBase<T> { public AdjacencyTreeBase(int entityId, int entityTypeId) { EntityId = entityId; EntityTypeId = entityTypeId; } public long? Id { get; set; } public int? SystemId { get; set; } public int EntityId { get; set; } public int EntityTypeId { get; set; } public bool? isActive { get; set; } public long? lft { get; set; } public long? rgt { get; set; } public abstract void AddChild(T c); public abstract void RemoveChild(T c); public abstract List<T> ListChildren(); public abstract void AddChildren(List<T> c); public abstract void ReplaceChildren(List<T> c); } public class AdjacencyTree<T> : AdjacencyTreeBase<T>abstract whereclass TAdjacencyTree : AdjacencyTree<T>AdjacencyTreeBase<AdjacencyTree> { private List<T>List<AdjacencyTree> _children = new List<T>List<AdjacencyTree>(); public List<T>List<AdjacencyTree> Children { get { return _children; } set { _children = value; } } public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } public override void AddChild(TAdjacencyTree component) { _children.Add(component); } public override void AddChildren(List<T>List<AdjacencyTree> c) { _children = c; } public override void ReplaceChildren(List<T>List<AdjacencyTree> c) { _children = c; } public override void RemoveChild(TAdjacencyTree component) { _children.Remove(component); } public override List<T>List<AdjacencyTree> ListChildren() { return _children; } } public class AdjacencyAgency : AdjacencyTree<AdjacencyAgency>AdjacencyTree { public string agency_name { get; set; } public string customer_number { get; set; } public string agency_type { get; set; } public AdjacencyAgency(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } } public class AdjacencyUser : AdjacencyTree<AdjacencyUser>AdjacencyTree { public string officer_number { get; set; } public string last_name { get; set; } public string first_name { get; set; } public string middle_initial { get; set; } public AdjacencyUser(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } } public class AdjacencyClient : AdjacencyTree<AdjacencyClient>AdjacencyTree { public string last_name { get; set; } public string first_name { get; set; } public string middle_initial { get; set; } public string ssn { get; set; } public AdjacencyClient(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } } But this doesn't seem to support yourThen CreateSample method. Can you please respondneeds to my comment under your question and I'll work onbe modified like this answer some more.:
public List<AdjacencyTree> CreateSample() { // build bottom of tree objects... var client1 = new AdjacencyClient(1, 4) { first_name = "Pic'nic", last_name = "Basket #1", ssn = "123-45-6789" }; var client2 = new AdjacencyClient(2, 4) { first_name = "Pic'nic", last_name = "Basket #2", ssn = "234-56-7890" }; var client3 = new AdjacencyClient(3, 4) { first_name = "Bear", last_name = "Cave", ssn = "345-67-8901" }; var client4 = new AdjacencyClient(4, 4) { first_name = "Picnic", last_name = "Table", ssn = "456-78-9012" }; // build the next level up and add the children... var officer1 = new AdjacencyUser(1, 3) { first_name = "Yogi", last_name = "Bear", officer_number = "YB123" }; officer1.AddChild(client1); officer1.AddChild(client2); var officer2 = new AdjacencyUser(2, 3) { first_name = "Park", last_name = "Ranger", officer_number = "PR123" }; officer2.AddChild(client3); officer2.AddChild(client4); // build the top of the tree and add the middle children... var agencyThatAlreadyExists = new AdjacencyAgency(1, 2) { agency_name = "Jellystone", agency_type = "Park", }; agencyThatAlreadyExists.AddChild(officer1); agencyThatAlreadyExists.AddChild(officer2); return new List<AdjacencyTree>() { agencyThatAlreadyExists }; }