Print Tree
Extension method .PrintTree() prints a tree representation of IIdentity.
// Get type
Type type = typeof(string);
// Create identity
IIdentity id = IdentityAccessors.Default.TypeName.Create(type);
// Print to string
string print = id.PrintTree();
// Output
Console.WriteLine(print);
Prints nodes from tail towards root.
Name("String") → Namespace("System") → Version("6.0.0.0") → Library("System.Private.CoreLib") → PublicKeyToken(7cec85d7bea7798e) → Culture("neutral")
'format:' can be provided as argument. There are variations:
- PrintFormat.DefaultVeryLong
- PrintFormat.DefaultLong
- PrintFormat.Default
- PrintFormat.DefaultShort
- PrintFormat.DefaultVeryShort
string print = id.PrintTree(format: IdentityPrintTreeExtensions.PrintFormat.DefaultVeryShort);
"String" → "System" → "6.0.0.0" → "System.Private.CoreLib" → 7cec85d7bea7798e → "neutral"
.PrintTreeTo(TextWriter) prints directly to TextWriter such as Console.Out.
id.PrintTreeTo(Console.Out);
Components-property is printed as tree branches.
IIdentity id = new Identity<string>
{
Label = "MyList",
Part = IdentityParts.Name,
LabelAccessor = LabelAccessors.Default.String,
Components = new IIdentity[] {
IdentityAccessors.Default.TypeNameWithoutAssembly.Create(typeof(int))
}
};
Name("MyList") └── [0] Name("Int32") → Namespace("System")
TypeName identity accessor deconstructs generic types as branching components.
Type type = typeof(Dictionary<string, IList<int>>);
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
Name("Dictionary`2") → Namespace("Generic") → Namespace("Collections") → Namespace("System") ├── [0] Name("String") → Namespace("System") └── [1] Name("IList`1") → Namespace("Generic") → Namespace("Collections") → Namespace("System") └── [0] Name("Int32") → Namespace("System")
.VisitTree() extension methods visits tree branches.
// Get type
Type type = typeof(Dictionary<string, IList<int>>);
// Create identity
IIdentity id = IdentityAccessors.Default.TypeName.Create(type);
// Visit tree branches
foreach (var line in id.VisitTree())
{
// Visit nodes
foreach (var node in line.Identity.GetParts())
{
// Print line
Console.WriteLine($"{line.Level} {node.Part} {node.LabelObject}");
}
}
Full Example
Full example
using System;
using System.Collections.Generic;
using Avalanche.Accessor;
using Avalanche.Identity;
using Avalanche.Service;
class printtree
{
public static void Run()
{
{
// <01>
// Get type
Type type = typeof(string);
// Create identity
IIdentity id = IdentityAccessors.Default.TypeName.Create(type);
// Print to string
string print = id.PrintTree();
// Output
Console.WriteLine(print);
// </01>
// <02>
id.PrintTreeTo(Console.Out);
// </02>
}
{
//
Type type = typeof(string);
//
IIdentity id = IdentityAccessors.Default.TypeName.Create(type);
// <03>
string print = id.PrintTree(format: IdentityPrintTreeExtensions.PrintFormat.DefaultVeryShort);
// </03>
Console.WriteLine(print);
}
{
// <04>
IIdentity id = new Identity<string>
{
Label = "MyList",
Part = IdentityParts.Name,
LabelAccessor = LabelAccessors.Default.String,
Components = new IIdentity[] {
IdentityAccessors.Default.TypeNameWithoutAssembly.Create(typeof(int))
}
};
// </04>
id.PrintTreeTo(Console.Out);
}
{
// <05>
Type type = typeof(Dictionary<string, IList<int>>);
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
// </05>
id.PrintTreeTo(Console.Out);
}
{
// <21>
// Get type
Type type = typeof(Dictionary<string, IList<int>>);
// Create identity
IIdentity id = IdentityAccessors.Default.TypeName.Create(type);
// Visit tree branches
foreach (var line in id.VisitTree())
{
// Visit nodes
foreach (var node in line.Identity.GetParts())
{
// Print line
Console.WriteLine($"{line.Level} {node.Part} {node.LabelObject}");
}
}
// </21>
}
}
}