Good evening, please help with the composition of the recursive fields of the class. Suppose I have a product of the class InfoObject, in which there is an attribute "Composition" of type Collection (in fact, a table), where the elements are InfoObject and may also have a composition!

The question is how do I create a composition from the class InfoObject, so that I recursively (and with recursion I have a very hard time) ran over the composition of each product.

that is, it will look something like this

InfoObject pvc = product.GetInfoObject("ActualVersion"); var compositionList = pvc.GetCollectionElements("ProductBaselineConfigurationItemList").ToArray(); foreach (var pr in compositionList) { InfoObject pv = pr.GetInfoObject("ActualVersion"); if (pv != null) { //Здесь должна быть рекурсия } } 

and sample composition template

 private abstract class Component { protected readonly string Name; protected Component(string name) { Name = name; } public abstract void Add(Component c); } private class CompositeComponent : Component { private readonly List<Component> _children = new List<Component>(); public CompositeComponent(string name) : base(name) { } public override void Add(Component component) { _children.Add(component); } } class EndComponent : Component { public EndComponent(string name) : base(name) { } public override void Add(Component c) { } } 

I would also be very grateful if you could help me realize the nesting of such products according to type 1.1, 1.2, 1.1.1, 1.2.1, 1.2.1.2 ...

    1 answer 1

    Here is an example of a recursive class (the code does not correspond to your case, but the essence is generally the same):

      class Component { public String Name { get; set; } public List<Component> Childrens { get; set; } public Component(String name) { Name = name; Childrens = new List<Component>(); } public static void RecursiveWork (Component component) { //выполняем какую-либо работу над текущим компонентом Console.WriteLine("processing " + component.Name + " ..."); //далее обрабатываем все дочерние компоненты foreach (Component child in component.Childrens) Component.RecursiveWork(child); } } class Program { static void Main(string[] args) { Component Root = new Component("Root"); Root.Childrens.Add(new Component("Root.1")); Root.Childrens.Add(new Component("Root.2")); Root.Childrens.Add(new Component("Root.3") { Childrens = new List<Component>() { new Component("Root.3.1"), new Component("Root.3.2"), new Component("Root.3.3") { Childrens = new List<Component>() { new Component("Root.3.3.1"), new Component("Root.3.3.2"), } } } }); Component.RecursiveWork(Root); } } 

    As for the numbered component numbering, a solution comes to mind through a static list:

     static List<int> currentComponentNumeration; 

    In the recursive function we do the following:

    • when we start processing child components, we add this list the number "1" (the numbering starts from 1)

    • when the next component is processed, the last element of the list is increased by 1

    • when the components are finished, delete the last item in the list

    Accordingly, to get the number of the current component, for example in the form of the string "2.1.3", it remains only to bypass this list in order