TypeName
IdentityAccessors.Default.TypeName handles complete assembly qualified type names.
// Get identity accessor
IIdentityAccessor<Type> identityAccessor = IdentityAccessors.Default.TypeName;
// Construct identity
IIdentity identity = identityAccessor.Create(typeof(string));
IdentityAccessors.Default.TypeNameWithoutAssembly handles type names without assembly parts.
// Get identity accessor
IIdentityAccessor<Type> identityAccessor = IdentityAccessors.Default.TypeNameWithoutAssembly;
Construction
TypeName.Create() constructs IIdentity from an instance of the native type.
// Construct identity
IIdentity identity = IdentityAccessors.Default.TypeName.Create(typeof(string));
"String" → "System" → "6.0.0.0" → "System.Private.CoreLib" → 7cec85d7bea7798e → "neutral"
TypeNameWithoutAssembly.Create() constructs without libarary parts.
// Construct identity
IIdentity identity = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(typeof(string));
"String" → "System"
Deconstruct deconstructs IIdentity back into native type.
// Create id
IIdentity id = Identities.CreateNamespace("System").AppendName("String").SetReadOnly();
// Deconstruct identity parts into native value
Type type = IdentityAccessors.Default.TypeName.Deconstruct(id);
Print and Parse
C# System.Type has three kinds of type names.
.Name is a simple name part without namespace.
// Get type
Type type = typeof(System.String);
// "String"
string name = type.Name;
.FullName is namespace+name without the assembly. This is context-dependent identity, library being the context.
// "System.String"
string fullname = type.FullName;
.AssemblyQualifiedName is a complete name with libary, version and culture. This identity can be used context-free.
// "System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"
string assemblyqualifiedName = type.AssemblyQualifiedName!;
IdentityAccessors.Default.TypeNameWithoutAssembly and IdentityAccessors.Default.TypeName parses from all of the string formats System.Type produces.
//
string typename = "System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
// Parse assembly qualified type name
IIdentity id1 = IdentityAccessors.Default.TypeName.MemoryAccessors.Parse(typename);
// Parse full name type name
IIdentity id2 = IdentityAccessors.Default.TypeNameWithoutAssembly.MemoryAccessors.Parse(typename);
// "Name("String") → Namespace("System") → Version("6.0.0.0") → Library("System.Private.CoreLib") → PublicKeyToken(7cec85d7bea7798e) → Culture("neutral")"
Console.WriteLine(id1);
// "Name("String") → Namespace("System")"
Console.WriteLine(id2);
TypeName prints C# type system compatible strings.
// Construct identity
IIdentity identity = IdentityAccessors.Default.TypeName.Create(typeof(string));
// "System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"
string assemblyQualified = IdentityAccessors.Default.TypeName.MemoryAccessors.Print(identity);
// "System.String"
string fullname = IdentityAccessors.Default.TypeNameWithoutAssembly.MemoryAccessors.Print(identity);
Arrays
Arrays are represented as "Array" part where and with Int32 label for array rank.
Type type = typeof(string[]);
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
Array[1] → Name("String") → Namespace("System")
Generic Parameters
Generic type parameters are constructed as IIdentity.Components.
//
Type type = typeof(Dictionary<string, int>);
//
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
Name("Dictionary`2") → Namespace("Generic") → Namespace("Collections") → Namespace("System") ├── [0] Name("String") → Namespace("System") └── [1] Name("Int32") → Namespace("System")
Inner Type
Inner types have multiple 'Name' parts. Generic type parameters are on the tailing 'Name' part.
// Create type
Type type = typeof(OuterPart<object>.InnerPart<int>);
// Create identity
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
// Print
Console.WriteLine(id.PrintTree());
public class OuterPart<T>
{
public class InnerPart<Y> { }
}
Name("InnerPart`1") → Name("OuterPart`1") ├── [0] Name("Object") → Namespace("System") └── [1] Name("Int32") → Namespace("System")
Other
Extension method .PrintTree() extends Type classes. This is used for verbose debug printing.
string print = typeof(IList<int>).PrintTree(assemblyQualified: false);
Name("IList`1") → Namespace("Generic") → Namespace("Collections") → Namespace("System") └── [0] Name("Int32") → Namespace("System")
Full Example
Full example
using System;
using System.Collections.Generic;
using Avalanche.Accessor;
using Avalanche.Identity;
using Avalanche.Serialization;
using Avalanche.Service;
using Avalanche.Utilities;
using Avalanche.Writer;
class typename
{
public static void Run()
{
{
// <02>
// Get identity accessor
IIdentityAccessor<Type> identityAccessor = IdentityAccessors.Default.TypeName;
// Construct identity
IIdentity identity = identityAccessor.Create(typeof(string));
// </02>
}
{
// <03>
// Get identity accessor
IIdentityAccessor<Type> identityAccessor = IdentityAccessors.Default.TypeNameWithoutAssembly;
// </03>
}
{
// <04>
// Construct identity
IIdentity identity = IdentityAccessors.Default.TypeName.Create(typeof(string));
// </04>
Console.WriteLine(identity.PrintTree(format: IdentityPrintTreeExtensions.PrintFormat.DefaultVeryShort));
}
{
// <04B>
// Construct identity
IIdentity identity = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(typeof(string));
// </04B>
Console.WriteLine(identity.PrintTree(format: IdentityPrintTreeExtensions.PrintFormat.DefaultVeryShort));
}
{
// <05>
// Create id
IIdentity id = Identities.CreateNamespace("System").AppendName("String").SetReadOnly();
// Deconstruct identity parts into native value
Type type = IdentityAccessors.Default.TypeName.Deconstruct(id);
// </05>
}
{
// <08>
string print = typeof(IList<int>).PrintTree(assemblyQualified: false);
// </08>
Console.WriteLine(print);
}
{
// <10>
//
Type type = typeof(Dictionary<string, int>);
//
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
// </10>
//
id.PrintTreeTo(Console.Out);
}
{
// <11>
// Create type
Type type = typeof(OuterPart<object>.InnerPart<int>);
// Create identity
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
// Print
Console.WriteLine(id.PrintTree());
// </11>
}
{
// <20>
Type type = typeof(string[]);
IIdentity id = IdentityAccessors.Default.TypeNameWithoutAssembly.Create(type);
// </20>
id.PrintTreeTo(Console.Out);
}
{
// <31>
// Get type
Type type = typeof(System.String);
// "String"
string name = type.Name;
// </31>
Console.WriteLine(name);
// <32>
// "System.String"
string fullname = type.FullName;
// </32>
Console.WriteLine(fullname);
// <33>
// "System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"
string assemblyqualifiedName = type.AssemblyQualifiedName!;
// </33>
Console.WriteLine(assemblyqualifiedName);
}
{
// <34>
//
string typename = "System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
// Parse assembly qualified type name
IIdentity id1 = IdentityAccessors.Default.TypeName.MemoryAccessors.Parse(typename);
// Parse full name type name
IIdentity id2 = IdentityAccessors.Default.TypeNameWithoutAssembly.MemoryAccessors.Parse(typename);
// "Name("String") → Namespace("System") → Version("6.0.0.0") → Library("System.Private.CoreLib") → PublicKeyToken(7cec85d7bea7798e) → Culture("neutral")"
Console.WriteLine(id1);
// "Name("String") → Namespace("System")"
Console.WriteLine(id2);
// </34>
}
{
// <35>
// Construct identity
IIdentity identity = IdentityAccessors.Default.TypeName.Create(typeof(string));
// "System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"
string assemblyQualified = IdentityAccessors.Default.TypeName.MemoryAccessors.Print(identity);
// "System.String"
string fullname = IdentityAccessors.Default.TypeNameWithoutAssembly.MemoryAccessors.Print(identity);
// </35>
}
}
}
// <12>
public class OuterPart<T>
{
public class InnerPart<Y> { }
}
// </12>