Hi,
Got a complex reflection question. Given the code below how would you implement the pseudo so that given an instance of Parent it will enumerate over the types Properties find child objects with a Property of the same type as Parent and set the reference to the provided p. Hope that makes sense. Also I need this to work with Generic lists as well. See below for sample object graph. After running this every Person in the child Pet instances will be the Parent instance.
public class ChildSetter<Parent>
{
public void Set(Parent p)
{
//pseudo
//var parentName = p.GetType().Name;
//foreach (var property in p.Properties)
//{
// if (!property.IsList)
// {
// if (property.ContainsProperty(parentName))
// property.Properties[parentName] = p;
// }
// else
// {
// if (property.ListType.ContainsProperty(parentName))
// {
// foreach (var item in property)
// {
// item.Properties[parentName] = p;
// }
// }
// }
//}
}
}
public class Person
{
public Pet Pet { get; set; }
public IList<Pet> Pets { get; set; }
}
public class Pet
{
public Person Person { get; set; }
}
A non generic example of this code is below:
public void Set(Person p)
{
p.Pet.Person = p;
foreach (var pet in p.Pets)
{
pet.Person = p;
}
}