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
TThe type of the object represented in the hierarchical structure.
- Inheritance
-
Wrapper<T>Hierarchy<T>
- Implements
-
IHierarchy<T>IWrapper<T>
- Inherited Members
- Extension Methods
Examples
The following example demonstrates how to build a tree structure using
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
trueif 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
trueif 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
indexint
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
instanceTThe 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
instanceTThe instance to a node in the hierarchical structure represents.
memberMemberInfoThe member from where
instancewas 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
instanceTThe instance to a node in the hierarchical structure represents.
instanceTypeTypeThe type of
instance.
Returns
- IHierarchy<T>
A reference to the newly added hierarchical node.
Exceptions
- ArgumentNullException
instanceTypeis 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
instanceTThe instance to a node in the hierarchical structure represents.
instanceTypeTypeThe type of
instance.memberMemberInfoThe member from where
instancewas referenced.
Returns
- IHierarchy<T>
A reference to the newly added hierarchical node.
Exceptions
- ArgumentNullException
instanceTypeis 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
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
pathResolverFunc<IHierarchy<T>, string>The function delegate path resolver.
Returns
Replace(T)
Allows for the instance on the current node to be replaced with a new instance.
public void Replace(T instance)
Parameters
instanceTThe 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
instanceTThe new instance to replace the original with.
instanceTypeTypeThe type of the new instance.