I have the following class structure (simplified) :
public class MyBaseType
{
public string InstanceName { get; set; }
public List<MyBaseType> MyBaseTypeList = new List<MyBaseType>();
public List<MyObject1> MyObject1List = new List<MyObject1>();
public List<MyObject2> MyObject2List = new List<MyObject2>();
// Other properties
}
public class MyObject1
{
public string SimpleName { get; set; }
public string Name { get; set; }
// Other properties
}
public class MyObject2
{
public string SimpleName { get; set; }
public string Name { get; set; }
// Other properties
}
As you can see, an instance of MyBaseType can contain a list of instances of the same type as well as a list of MyObject1 and MyObject2.
A typical use for me of this would render a structure as shown here :
MyBaseType instance1
|-> InstanceName = "instance1"
|
|-> MyBaseType instance2
| |-> InstanceName = "instance2"
| |
| |-> MyObject1 object1_1
| | |-> SimpleName = "object1_1"
| |-> MyObject2 object2_1
| | |-> SimpleName = "object2_1"
|
|-> MyBaseType instance3
| |-> InstanceName = "instance3"
| |
| |-> MyObject1 object1_2
| | |-> SimpleName = "object1_2"
| |-> MyObject2 object2_2
| | |-> SimpleName = "object2_1"
|
|-> MyObject1 object1_3
| |-> SimpleName = "object1_3"
|-> MyObject2 object2_3
| |-> SimpleName = "object2_3"
Visibly, this structure can get quite complex.
What I need to do however is to recursively dive into the MyBaseType list, track all the InstanceName values as I go to finally append the combined string with the SimpleName of either MyObject1 or MyObject2 to the Name property of either object.
Following the above structure example, the names would be :
MyObject1 object1_1 : instance1_instance2_object1_1
MyObject1 object2_1 : instance1_instance2_object2_1
MyObject1 object1_2 : instance1_instance3_object1_1
MyObject1 object2_2 : instance1_instance3_object1_2
MyObject1 object1_3 : instance1_object1_3
MyObject1 object2_3 : instance1_object2_3
I am struggling to find a way to recursively iterate through the List<MyBaseType> AND keep track of the name being constructed.
for loop. Also if you want to make sure a property of base type is set. You can force its instantiation through a constructor if you have to. I think you are a bit confused with properties that self reference the object. Like a node class from a linked list. The answer provided is just a for loop but hidden. You have to read what linq is all about.