Table of Contents

Hierarchy Class

Definition

Namespace
Cuemon.Extensions.Runtime
Assemblies
Cuemon.Extensions.Core.dll
Source
src/Cuemon.Extensions.Core/Runtime/Hierarchy.cs

Provides a set of static methods for hierarchy related operations.

public static class Hierarchy
Inheritance
Hierarchy

Examples

The following example demonstrates how to build and traverse a hierarchical tree structure using Hierarchy. It shows adding nodes, printing paths, searching for nodes with Find, and generating an object hierarchy from an anonymous type.

using System;
using System.Linq;
using Cuemon.Extensions.Runtime;

namespace Cuemon.Extensions.Runtime;

public class HierarchyExample
{
    public void Demonstrate()
    {
        var root = new Hierarchy<string>();
        root.Add("root");

        var child1 = root.Add("department");
        var child2 = root.Add("team");

        child1.Add("employee1");
        child1.Add("employee2");

        Console.WriteLine(root.GetPath());
        Console.WriteLine(child1.GetPath(n => n.Instance.ToString().ToUpper()));

        var matches = Hierarchy.Find(root, n => n.Instance.ToString().StartsWith("employee"));
        Console.WriteLine($"Found {matches.Count()} node(s)");

        var obj = new { Name = "Root", Items = new[] { new { Id = 1 }, new { Id = 2 } } };
        IHierarchy<object> tree = Hierarchy.GetObjectHierarchy(obj);
        Console.WriteLine(tree.GetPath(n => n.InstanceType.Name));
    }
}

Methods

Name Description
Find<T>(IHierarchy<T>, Func<IHierarchy<T>, bool>)

Retrieves all nodes that match the conditions defined by the function delegate match.

GetObjectHierarchy(object, Action<HierarchyOptions>)

Gets the tree structure of the specified source wrapped in an IHierarchy<T> node representing a hierarchical structure.

TraverseWhileNotEmpty<TSource>(TSource, Func<TSource, IEnumerable<TSource>>)

Invokes the specified traversal path of source until obstructed by an empty sequence.

TraverseWhileNotNull<TSource>(TSource, Func<TSource, TSource>)

Invokes the specified traversal path of source until obstructed by a null value.