Table of Contents

Hierarchy<T> Class

Definition

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

Represents a way to expose a node of a hierarchical structure, including the node object of type T.

public sealed class Hierarchy<T> : Wrapper<T>, IHierarchy<T>, IWrapper<T>, IData

Type Parameters

T

The type of the object represented in the hierarchical structure.

Inheritance
object
Hierarchy<T>
Implements
Inherited Members
Extension Methods

Examples

The following example demonstrates how to build a tree structure using and navigate nodes through parent-child relationships, depth, index, and path.

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

namespace MyApp.Examples;

public class HierarchyOfTExample
{
    public void Demonstrate()
    {
        var root = new Hierarchy<string>();
        var rootNode = root.Add("Root");
        var child = root.Add("Child");
        var grandchild = child.Add("Grandchild");
        var sibling = root.Add("Sibling");

        Console.WriteLine($"Root depth: {root.Depth}, index: {root.Index}");       // 0, 0
        Console.WriteLine($"Child depth: {child.Depth}, index: {child.Index}");    // 1, 1
        Console.WriteLine($"Grandchild depth: {grandchild.Depth}");                 // 2
        Console.WriteLine($"Path: {grandchild.GetPath()}");                         // Root.Child.Grandchild
        Console.WriteLine($"HasChildren: {root.HasChildren}");                      // True
        Console.WriteLine($"HasParent: {child.HasParent}");                         // True

        // Retrieve via indexer
        Console.WriteLine(root[0].Instance); // Root
        Console.WriteLine(root[2].Instance); // Grandchild

        // Enumerate children
        foreach (var node in root.GetChildren())
        {
            Console.WriteLine(node.Instance);
        // Output: Child, Sibling

}}
}

Constructors

Name Description
Hierarchy()

Initializes a new instance of the Hierarchy<T> class.

Properties

Name Description
Depth

Gets the current depth of the node in the hierarchical structure.

HasChildren

Gets a value indicating whether this instance has any children.

HasParent

Gets a value indicating whether this instance has a parent.

Index

Gets the zero-based index of the current node that this hierarchical structure represents.

this[int]

Gets the node at the specified index.

Methods

Name Description
Add(T)

Adds the specified instance to a node in the hierarchical structure representation.

Add(T, MemberInfo)

Adds the specified instance to a node in the hierarchical structure representation.

Add(T, Type)

Adds the specified instance to a node in the hierarchical structure representation.

Add(T, Type, MemberInfo)

Adds the specified instance to a node in the hierarchical structure representation.

GetChildren()

Gets an IEnumerable<T> sequence that represents all the child nodes of the current hierarchical node.

GetParent()

Gets the parent node of the current node in the hierarchical structure.

GetPath()

Gets the hierarchical path of the node in the hierarchical structure.

GetPath(Func<IHierarchy<T>, string>)

Gets the hierarchical path of the node in the hierarchical structure.

Replace(T)

Allows for the instance on the current node to be replaced with a new instance.

Replace(T, Type)

Allows for the instance on the current node to be replaced with a new instance.