Table of Contents

Class Hierarchy<T>

Namespace
Cuemon.Extensions.Runtime
Assembly
Cuemon.Extensions.Core.dll

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
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

Hierarchy()

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

public Hierarchy()

Properties

Depth

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

public int Depth { get; }

Property Value

int

The current depth of the in the hierarchical structure.

HasChildren

Gets a value indicating whether this instance has any children.

public bool HasChildren { get; }

Property Value

bool

true if this instance has any children; otherwise, false.

HasParent

Gets a value indicating whether this instance has a parent.

public bool HasParent { get; }

Property Value

bool

true if this instance has a parent; otherwise, false.

Index

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

public int Index { get; }

Property Value

int

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

this[int]

Gets the node at the specified index.

public IHierarchy<T> this[int index] { get; }

Parameters

index int

Property Value

IHierarchy<T>

The node at the specified index.

Methods

Add(T)

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

public IHierarchy<T> Add(T instance)

Parameters

instance T

The instance to a node in the hierarchical structure represents.

Returns

IHierarchy<T>

A reference to the newly added hierarchical node.

Add(T, MemberInfo)

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

public IHierarchy<T> Add(T instance, MemberInfo member)

Parameters

instance T

The instance to a node in the hierarchical structure represents.

member MemberInfo

The member from where instance was referenced.

Returns

IHierarchy<T>

A reference to the newly added hierarchical node.

Add(T, Type)

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

public IHierarchy<T> Add(T instance, Type instanceType)

Parameters

instance T

The instance to a node in the hierarchical structure represents.

instanceType Type

The type of instance.

Returns

IHierarchy<T>

A reference to the newly added hierarchical node.

Exceptions

ArgumentNullException

instanceType is null.

Add(T, Type, MemberInfo)

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

public IHierarchy<T> Add(T instance, Type instanceType, MemberInfo member)

Parameters

instance T

The instance to a node in the hierarchical structure represents.

instanceType Type

The type of instance.

member MemberInfo

The member from where instance was referenced.

Returns

IHierarchy<T>

A reference to the newly added hierarchical node.

Exceptions

ArgumentNullException

instanceType is null.

GetChildren()

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

public IEnumerable<IHierarchy<T>> GetChildren()

Returns

IEnumerable<IHierarchy<T>>

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.

public IHierarchy<T> GetParent()

Returns

IHierarchy<T>

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

GetPath()

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

public string GetPath()

Returns

string

A string that identifies the hierarchical path relative to the current node.

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

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

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

Parameters

pathResolver Func<IHierarchy<T>, string>

The function delegate path resolver.

Returns

string

A string that identifies the hierarchical path relative to the current node.

Replace(T)

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

public void Replace(T instance)

Parameters

instance T

The new instance to replace the original with.

Replace(T, Type)

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

public void Replace(T instance, Type instanceType)

Parameters

instance T

The new instance to replace the original with.

instanceType Type

The type of the new instance.