Repository navigation
OnixLabs.Core.Reflection.TypeExtensions
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
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class TypeExtensionsBecause 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.
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,
UseNullableShorthandTypeNamestakes precedence overUseGenericTypeArgumentsforNullable<T>types,UseValueTupleSyntaxtakes precedence overUseGenericTypeArgumentsfor value tuples, andUseValueTupleNamestakes precedence over both. SeeTypeDeclarationFlagsfor the full precedence rules.
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>"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)"| Method | Returns | Description |
|---|---|---|
GetCSharpTypeDeclaration(Type, TypeDeclarationFlags) |
string |
Gets the C# type declaration for the current Type instance. |
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. |
- OnixLabs.Core.Reflection (namespace overview)
- Home
- TypeDeclarationFlags
- OnixLabs.Core on NuGet
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions