Nice formatting of type names

Add extension to System.Type that returns the name of type including generic arguments in a nice readable format. It returns only short names of generic type and type arguments.

Copy Source
Copy Link
Tools:
 1: type System.Type with 
 2:   /// Returns nicely formatted name of the type
 3:   member t.NiceName =
 4:     let sb = new System.Text.StringBuilder()
 5:     let rec build (t:System.Type) =
 6:       if t.IsGenericType then 
 7:         // Remove the `1 part from generic names
 8:         let tick = t.Name.IndexOf('`')
 9:         let name = t.Name.Substring(0, tick) 
10:         Printf.bprintf sb "%s" t.Name
11:         Printf.bprintf sb "<"
12:         // Print generic type arguments recursively
13:         let args = t.GetGenericArguments()
14:         for i in 0 .. args.Length - 1 do 
15:           if i <> 0 then Printf.bprintf sb ", "
16:           build args.[i]
17:         Printf.bprintf sb ">"
18:       else
19:         // Print ordiary type name
20:         Printf.bprintf sb "%s" t.Name
21:     build t
22:     sb.ToString()
23: 
namespace System
type Type =
  class
    inherit System.Reflection.MemberInfo
    member Assembly : System.Reflection.Assembly
    member AssemblyQualifiedName : string
    member Attributes : System.Reflection.TypeAttributes
    member BaseType : System.Type
    member ContainsGenericParameters : bool
    member DeclaringMethod : System.Reflection.MethodBase
    member DeclaringType : System.Type
    member Equals : obj -> bool
    member Equals : System.Type -> bool
    member FindInterfaces : System.Reflection.TypeFilter * obj -> System.Type []
    member FindMembers : System.Reflection.MemberTypes * System.Reflection.BindingFlags * System.Reflection.MemberFilter * obj -> System.Reflection.MemberInfo []
    member FullName : string
    member GUID : System.Guid
    member GenericParameterAttributes : System.Reflection.GenericParameterAttributes
    member GenericParameterPosition : int
    member GetArrayRank : unit -> int
    member GetConstructor : System.Type [] -> System.Reflection.ConstructorInfo
    member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.ConstructorInfo
    member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.ConstructorInfo
    member GetConstructors : unit -> System.Reflection.ConstructorInfo []
    member GetConstructors : System.Reflection.BindingFlags -> System.Reflection.ConstructorInfo []
    member GetDefaultMembers : unit -> System.Reflection.MemberInfo []
    member GetElementType : unit -> System.Type
    member GetEnumName : obj -> string
    member GetEnumNames : unit -> string []
    member GetEnumUnderlyingType : unit -> System.Type
    member GetEnumValues : unit -> System.Array
    member GetEvent : string -> System.Reflection.EventInfo
    member GetEvent : string * System.Reflection.BindingFlags -> System.Reflection.EventInfo
    member GetEvents : unit -> System.Reflection.EventInfo []
    member GetEvents : System.Reflection.BindingFlags -> System.Reflection.EventInfo []
    member GetField : string -> System.Reflection.FieldInfo
    member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfo
    member GetFields : unit -> System.Reflection.FieldInfo []
    member GetFields : System.Reflection.BindingFlags -> System.Reflection.FieldInfo []
    member GetGenericArguments : unit -> System.Type []
    member GetGenericParameterConstraints : unit -> System.Type []
    member GetGenericTypeDefinition : unit -> System.Type
    member GetHashCode : unit -> int
    member GetInterface : string -> System.Type
    member GetInterface : string * bool -> System.Type
    member GetInterfaceMap : System.Type -> System.Reflection.InterfaceMapping
    member GetInterfaces : unit -> System.Type []
    member GetMember : string -> System.Reflection.MemberInfo []
    member GetMember : string * System.Reflection.BindingFlags -> System.Reflection.MemberInfo []
    member GetMember : string * System.Reflection.MemberTypes * System.Reflection.BindingFlags -> System.Reflection.MemberInfo []
    member GetMembers : unit -> System.Reflection.MemberInfo []
    member GetMembers : System.Reflection.BindingFlags -> System.Reflection.MemberInfo []
    member GetMethod : string -> System.Reflection.MethodInfo
    member GetMethod : string * System.Type [] -> System.Reflection.MethodInfo
    member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
    member GetMethod : string * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.MethodInfo
    member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.MethodInfo
    member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.MethodInfo
    member GetMethods : unit -> System.Reflection.MethodInfo []
    member GetMethods : System.Reflection.BindingFlags -> System.Reflection.MethodInfo []
    member GetNestedType : string -> System.Type
    member GetNestedType : string * System.Reflection.BindingFlags -> System.Type
    member GetNestedTypes : unit -> System.Type []
    member GetNestedTypes : System.Reflection.BindingFlags -> System.Type []
    member GetProperties : unit -> System.Reflection.PropertyInfo []
    member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo []
    member GetProperty : string -> System.Reflection.PropertyInfo
    member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
    member GetProperty : string * System.Type [] -> System.Reflection.PropertyInfo
    member GetProperty : string * System.Type -> System.Reflection.PropertyInfo
    member GetProperty : string * System.Type * System.Type [] -> System.Reflection.PropertyInfo
    member GetProperty : string * System.Type * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.PropertyInfo
    member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Type * System.Type [] * System.Reflection.ParameterModifier [] -> System.Reflection.PropertyInfo
    member GetType : unit -> System.Type
    member HasElementType : bool
    member InvokeMember : string * System.Reflection.BindingFlags * System.Reflection.Binder * obj * obj [] -> obj
    member InvokeMember : string * System.Reflection.BindingFlags * System.Reflection.Binder * obj * obj [] * System.Globalization.CultureInfo -> obj
    member InvokeMember : string * System.Reflection.BindingFlags * System.Reflection.Binder * obj * obj [] * System.Reflection.ParameterModifier [] * System.Globalization.CultureInfo * string [] -> obj
    member IsAbstract : bool
    member IsAnsiClass : bool
    member IsArray : bool
    member IsAssignableFrom : System.Type -> bool
    member IsAutoClass : bool
    member IsAutoLayout : bool
    member IsByRef : bool
    member IsCOMObject : bool
    member IsClass : bool
    member IsContextful : bool
    member IsEnum : bool
    member IsEnumDefined : obj -> bool
    member IsEquivalentTo : System.Type -> bool
    member IsExplicitLayout : bool
    member IsGenericParameter : bool
    member IsGenericType : bool
    member IsGenericTypeDefinition : bool
    member IsImport : bool
    member IsInstanceOfType : obj -> bool
    member IsInterface : bool
    member IsLayoutSequential : bool
    member IsMarshalByRef : bool
    member IsNested : bool
    member IsNestedAssembly : bool
    member IsNestedFamANDAssem : bool
    member IsNestedFamORAssem : bool
    member IsNestedFamily : bool
    member IsNestedPrivate : bool
    member IsNestedPublic : bool
    member IsNotPublic : bool
    member IsPointer : bool
    member IsPrimitive : bool
    member IsPublic : bool
    member IsSealed : bool
    member IsSecurityCritical : bool
    member IsSecuritySafeCritical : bool
    member IsSecurityTransparent : bool
    member IsSerializable : bool
    member IsSpecialName : bool
    member IsSubclassOf : System.Type -> bool
    member IsUnicodeClass : bool
    member IsValueType : bool
    member IsVisible : bool
    member MakeArrayType : unit -> System.Type
    member MakeArrayType : int -> System.Type
    member MakeByRefType : unit -> System.Type
    member MakeGenericType : System.Type [] -> System.Type
    member MakePointerType : unit -> System.Type
    member MemberType : System.Reflection.MemberTypes
    member Module : System.Reflection.Module
    member Namespace : string
    member ReflectedType : System.Type
    member StructLayoutAttribute : System.Runtime.InteropServices.StructLayoutAttribute
    member ToString : unit -> string
    member TypeHandle : System.RuntimeTypeHandle
    member TypeInitializer : System.Reflection.ConstructorInfo
    member UnderlyingSystemType : System.Type
    static val FilterAttribute : System.Reflection.MemberFilter
    static val FilterName : System.Reflection.MemberFilter
    static val FilterNameIgnoreCase : System.Reflection.MemberFilter
    static val Missing : obj
    static val Delimiter : char
    static val EmptyTypes : System.Type []
    static member DefaultBinder : System.Reflection.Binder
    static member GetType : string -> System.Type
    static member GetType : string * bool -> System.Type
    static member GetType : string * bool * bool -> System.Type
    static member GetType : string * System.Func<System.Reflection.AssemblyName,System.Reflection.Assembly> * System.Func<System.Reflection.Assembly,string,bool,System.Type> -> System.Type
    static member GetType : string * System.Func<System.Reflection.AssemblyName,System.Reflection.Assembly> * System.Func<System.Reflection.Assembly,string,bool,System.Type> * bool -> System.Type
    static member GetType : string * System.Func<System.Reflection.AssemblyName,System.Reflection.Assembly> * System.Func<System.Reflection.Assembly,string,bool,System.Type> * bool * bool -> System.Type
    static member GetTypeArray : obj [] -> System.Type []
    static member GetTypeCode : System.Type -> System.TypeCode
    static member GetTypeFromCLSID : System.Guid -> System.Type
    static member GetTypeFromCLSID : System.Guid * bool -> System.Type
    static member GetTypeFromCLSID : System.Guid * string -> System.Type
    static member GetTypeFromCLSID : System.Guid * string * bool -> System.Type
    static member GetTypeFromHandle : System.RuntimeTypeHandle -> System.Type
    static member GetTypeFromProgID : string -> System.Type
    static member GetTypeFromProgID : string * bool -> System.Type
    static member GetTypeFromProgID : string * string -> System.Type
    static member GetTypeFromProgID : string * string * bool -> System.Type
    static member GetTypeHandle : obj -> System.RuntimeTypeHandle
    static member ReflectionOnlyGetType : string * bool * bool -> System.Type
  end

Full name: System.Type

  type: System.Type
  implements: System.Reflection.ICustomAttributeProvider
  implements: System.Runtime.InteropServices._MemberInfo
  implements: System.Runtime.InteropServices._Type
  implements: System.Reflection.IReflect
  inherits: System.Reflection.MemberInfo
val t : System.Type

  type: System.Type
  implements: System.Reflection.ICustomAttributeProvider
  implements: System.Runtime.InteropServices._MemberInfo
  implements: System.Runtime.InteropServices._Type
  implements: System.Reflection.IReflect
  inherits: System.Reflection.MemberInfo
member System.Type.NiceName : string

Full name: Test.NiceName

Returns nicely formatted name of the type
val sb : System.Text.StringBuilder

  type: System.Text.StringBuilder
  implements: System.Runtime.Serialization.ISerializable
namespace System.Text
type StringBuilder =
  class
    new : unit -> System.Text.StringBuilder
    new : int -> System.Text.StringBuilder
    new : string -> System.Text.StringBuilder
    new : string * int -> System.Text.StringBuilder
    new : string * int * int * int -> System.Text.StringBuilder
    new : int * int -> System.Text.StringBuilder
    member Append : string -> System.Text.StringBuilder
    member Append : bool -> System.Text.StringBuilder
    member Append : System.SByte -> System.Text.StringBuilder
    member Append : System.Byte -> System.Text.StringBuilder
    member Append : char -> System.Text.StringBuilder
    member Append : int16 -> System.Text.StringBuilder
    member Append : int -> System.Text.StringBuilder
    member Append : int64 -> System.Text.StringBuilder
    member Append : float32 -> System.Text.StringBuilder
    member Append : float -> System.Text.StringBuilder
    member Append : decimal -> System.Text.StringBuilder
    member Append : uint16 -> System.Text.StringBuilder
    member Append : uint32 -> System.Text.StringBuilder
    member Append : uint64 -> System.Text.StringBuilder
    member Append : obj -> System.Text.StringBuilder
    member Append : char [] -> System.Text.StringBuilder
    member Append : char * int -> System.Text.StringBuilder
    member Append : char [] * int * int -> System.Text.StringBuilder
    member Append : string * int * int -> System.Text.StringBuilder
    member AppendFormat : string * obj -> System.Text.StringBuilder
    member AppendFormat : string * obj [] -> System.Text.StringBuilder
    member AppendFormat : string * obj * obj -> System.Text.StringBuilder
    member AppendFormat : System.IFormatProvider * string * obj [] -> System.Text.StringBuilder
    member AppendFormat : string * obj * obj * obj -> System.Text.StringBuilder
    member AppendLine : unit -> System.Text.StringBuilder
    member AppendLine : string -> System.Text.StringBuilder
    member Capacity : int with get, set
    member Chars : int -> char with get, set
    member Clear : unit -> System.Text.StringBuilder
    member CopyTo : int * char [] * int * int -> unit
    member EnsureCapacity : int -> int
    member Equals : System.Text.StringBuilder -> bool
    member Insert : int * string -> System.Text.StringBuilder
    member Insert : int * bool -> System.Text.StringBuilder
    member Insert : int * System.SByte -> System.Text.StringBuilder
    member Insert : int * System.Byte -> System.Text.StringBuilder
    member Insert : int * int16 -> System.Text.StringBuilder
    member Insert : int * char -> System.Text.StringBuilder
    member Insert : int * char [] -> System.Text.StringBuilder
    member Insert : int * int -> System.Text.StringBuilder
    member Insert : int * int64 -> System.Text.StringBuilder
    member Insert : int * float32 -> System.Text.StringBuilder
    member Insert : int * float -> System.Text.StringBuilder
    member Insert : int * decimal -> System.Text.StringBuilder
    member Insert : int * uint16 -> System.Text.StringBuilder
    member Insert : int * uint32 -> System.Text.StringBuilder
    member Insert : int * uint64 -> System.Text.StringBuilder
    member Insert : int * obj -> System.Text.StringBuilder
    member Insert : int * string * int -> System.Text.StringBuilder
    member Insert : int * char [] * int * int -> System.Text.StringBuilder
    member Length : int with get, set
    member MaxCapacity : int
    member Remove : int * int -> System.Text.StringBuilder
    member Replace : string * string -> System.Text.StringBuilder
    member Replace : char * char -> System.Text.StringBuilder
    member Replace : string * string * int * int -> System.Text.StringBuilder
    member Replace : char * char * int * int -> System.Text.StringBuilder
    member ToString : unit -> string
    member ToString : int * int -> string
  end

Full name: System.Text.StringBuilder

  type: System.Text.StringBuilder
  implements: System.Runtime.Serialization.ISerializable
val build : (System.Type -> unit)
property System.Type.IsGenericType: bool
val tick : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
property System.Reflection.MemberInfo.Name: string
Multiple overloads
System.String.IndexOf(value: string) : int
System.String.IndexOf(value: char) : int
System.String.IndexOf(value: string, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int) : int
System.String.IndexOf(value: char, startIndex: int) : int
System.String.IndexOf(value: string, startIndex: int, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int, count: int) : int
System.String.IndexOf(value: char, startIndex: int, count: int) : int
System.String.IndexOf(value: string, startIndex: int, count: int, comparisonType: System.StringComparison) : int
val name : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
Multiple overloads
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
module Printf

from Microsoft.FSharp.Core
val bprintf : System.Text.StringBuilder -> Printf.BuilderFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.Printf.bprintf
val args : System.Type []

  type: System.Type []
  implements: System.ICloneable
  implements: System.Collections.IList
  implements: System.Collections.ICollection
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.IStructuralEquatable
  implements: System.Collections.Generic.IList<System.Type>
  implements: System.Collections.Generic.ICollection<System.Type>
  implements: seq<System.Type>
  implements: System.Collections.IEnumerable
  inherits: System.Array
System.Type.GetGenericArguments() : System.Type []
val i : int32

  type: int32
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
property System.Array.Length: int
Multiple overloads
System.Object.ToString() : string
System.Text.StringBuilder.ToString(startIndex: int, length: int) : string

More information

Link: http://fssnip.net/E
Posted: 3 years ago
Author: Tomas Petricek (website)
Tags: extension, system.type, formatting