I have the following that needs to be converted to a multi level hierarchical tree.
public enum ERootType { NotUsed, RootType1, RootType2, }
public enum ESubType { NotUsed, SubTypeA, SubTypeB, SubTypeC, }
public class Payload { }
public class MyData
{
public MyData(ERootType rootType, ESubType subType,
string displayName, Payload payload)
{
RootType = rootType;
SubType = subType;
DisplayName = displayName;
Payload = payload;
}
public ERootType RootType { get; }
public ESubType SubType { get; }
public string DisplayName { get; set; }
public object Payload { get; }
public object this[string propertyName]
{
get
{
Type myType = typeof(MyData);
PropertyInfo myPropInfo = myType.GetProperty(propertyName);
return myPropInfo.GetValue(this, null);
}
set
{
Type myType = typeof(MyData);
PropertyInfo myPropInfo = myType.GetProperty(propertyName);
myPropInfo.SetValue(this, value, null);
}
}
}
// Create and initialize flat list of data
var Resources = new List<MyData>()
{
new MyData(ERootType.RootType1, ESubType.SubTypeA, "Item1", new Payload()),
new MyData(ERootType.RootType1, ESubType.SubTypeA, "Item2", new Payload()),
new MyData(ERootType.RootType1, ESubType.SubTypeB, "Item3", new Payload()),
new MyData(ERootType.RootType1, ESubType.SubTypeB, "Item4", new Payload()),
new MyData(ERootType.RootType1, ESubType.SubTypeC, "Item5", new Payload()),
new MyData(ERootType.RootType2, ESubType.SubTypeA, "Item6", new Payload()),
new MyData(ERootType.RootType2, ESubType.SubTypeA, "Item7", new Payload()),
new MyData(ERootType.RootType2, ESubType.SubTypeB, "Item8", new Payload()),
new MyData(ERootType.RootType2, ESubType.SubTypeB, "Item9", new Payload()),
new MyData(ERootType.RootType2, ESubType.SubTypeC, "Item10", new Payload()),
};
I need the above data converted to a hierarchical list that looks something like the following.
- RootType1 (ERootType = RootType1, ESubType = NotUsed, DiaplayName = "RootType1", Payload = null)
- SubTypeA (ERootType = NotUsed, ESubType = SubTypeA, DiaplayName = "SubTypeA", Payload = null)
- Item1 (ERootType = RootType1, ESubType = SubTypeA, DiaplayName = "Item1", Payload = payload object instance)
- Item2
- SubTypeB
- Item3
- Item4
- SubTypeC
- Item5
- RootType2
- SubTypeA
- Item6
- Item7
- SubTypeB
- Item8
- Item9
- SubTypeC
- Item10
I was trying to come up with a solution using Generics and Linq. I created the following class to support the hierarchical results.
public class TreeItem<T>
{
public T Item { get; set; }
public IEnumerable<TreeItem<T>> Children { get; set; }
}
I was thinking that it would be good to have a generic method that could generate a tree with n levels by passing in an array of levels which are the names of the properties in the class being converted. I was thinking this method would use recursion. Here's a function declaration I was playing with.
public static IEnumerable<TreeItem<T>> GenerateNLevelTree<T>(this IEnumerable<T> collection,
string[] levelProps, int currentLevel = 0)
The class MyData contains a contains an indexer to support accessing the each property of MyData using a string.
Any help with a solution for generating the hierarchical data structure would be greatly appreciated.