Skip to content

OnixLabs.Core.Reflection.TypeExtensions

matthew edited this page May 27, 2026 · 1 revision

TypeExtensions Class

Provides extension methods over System.Type for rendering a Type as a C# type declaration string, with configurable formatting controlled by TypeDeclarationFlags.

Namespace OnixLabs.Core.Reflection
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → TypeExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class TypeExtensions

Because the type is decorated with [EditorBrowsable(EditorBrowsableState.Never)], the extension method is hidden from IntelliSense by default in editors that honour the attribute, though it remains fully callable.

Remarks

TypeExtensions exposes a single extension method, GetCSharpTypeDeclaration, which converts a Type into a human-readable C# type declaration string. The exact form of the output is controlled by the TypeDeclarationFlags bit field passed to the method.

The default reflection name returned by Type.Name is often unsuitable for display: generic types carry an arity suffix (for example, List`1), generic arguments are not rendered, and there is no support for C# language aliases (int rather than Int32), nullable shorthand (int? rather than Nullable<Int32>), or value tuple syntax ((string Item1, int Item2)). GetCSharpTypeDeclaration addresses these cases, producing output that resembles how the type would be written in C# source.

The actual formatting is performed by an internal type declaration formatter; only the public extension method is part of the supported surface.

Note Several flags supersede others when combined. For example, UseNullableShorthandTypeNames takes precedence over UseGenericTypeArguments for Nullable<T> types, UseValueTupleSyntax takes precedence over UseGenericTypeArguments for value tuples, and UseValueTupleNames takes precedence over both. See TypeDeclarationFlags for the full precedence rules.

Examples

Formatting a type declaration

using System;
using System.Collections.Generic;
using OnixLabs.Core.Reflection;

// Simple CLR type name (the default).
string a = typeof(int).GetCSharpTypeDeclaration();                                   // "Int32"

// C# language alias.
string b = typeof(int).GetCSharpTypeDeclaration(TypeDeclarationFlags.UseAliasedTypeNames); // "int"

// Generic type with its generic arguments.
string c = typeof(List<int>).GetCSharpTypeDeclaration(TypeDeclarationFlags.UseGenericTypeArguments); // "List<Int32>"

// Fully expanded: namespace-qualified, aliased, nullable shorthand, generics and tuples.
string d = typeof(Dictionary<string, int>).GetCSharpTypeDeclaration(TypeDeclarationFlags.All);
// "System.Collections.Generic.Dictionary<string, int>"

Nullable shorthand and value tuples

using OnixLabs.Core.Reflection;

string nullable = typeof(int?).GetCSharpTypeDeclaration(
    TypeDeclarationFlags.UseNullableShorthandTypeNames | TypeDeclarationFlags.UseAliasedTypeNames);
// "int?"

string tuple = typeof((string, int)).GetCSharpTypeDeclaration(TypeDeclarationFlags.All);
// "(string Item1, int Item2)"

Methods

Extension Methods

Method Returns Description
GetCSharpTypeDeclaration(Type, TypeDeclarationFlags) string Gets the C# type declaration for the current Type instance.

GetCSharpTypeDeclaration(Type, TypeDeclarationFlags)

public static string GetCSharpTypeDeclaration(this Type type, TypeDeclarationFlags flags = default)

Gets the type declaration for the current Type instance.

Depending on the specified TypeDeclarationFlags, this method is capable of returning type declarations including simple type names, namespace-qualified type names, aliased type names, nullable shorthand notation, generic arguments, and value tuples.

Parameter Type Description
type Type The current Type instance from which to obtain the type declaration.
flags TypeDeclarationFlags The flags that specify how the type declaration should be formatted. Defaults to None, which produces the simple CLR type name.

Returns — The type declaration for the current Type instance.

Exceptions

Exception Condition
ArgumentNullException type is null.

See Also

ONIXLabs .NET Library


Clone this wiki locally