21 people like it.

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.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
type System.Type with 
  /// Returns nicely formatted name of the type
  member t.NiceName =
    let sb = new System.Text.StringBuilder()
    let rec build (t:System.Type) =
      if t.IsGenericType then 
        // Remove the `1 part from generic names
        let tick = t.Name.IndexOf('`')
        let name = t.Name.Substring(0, tick) 
        Printf.bprintf sb "%s" t.Name
        Printf.bprintf sb "<"
        // Print generic type arguments recursively
        let args = t.GetGenericArguments()
        for i in 0 .. args.Length - 1 do 
          if i <> 0 then Printf.bprintf sb ", "
          build args.[i]
        Printf.bprintf sb ">"
      else
        // Print ordiary type name
        Printf.bprintf sb "%s" t.Name
    build t
    sb.ToString()
namespace System
type Type =
  inherit MemberInfo
  member Assembly : Assembly
  member AssemblyQualifiedName : string
  member Attributes : TypeAttributes
  member BaseType : Type
  member ContainsGenericParameters : bool
  member DeclaringMethod : MethodBase
  member DeclaringType : Type
  member Equals : o:obj -> bool + 1 overload
  member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
  member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
  ...

Full name: System.Type
val t : System.Type
member System.Type.NiceName : string

Full name: Script.NiceName


 Returns nicely formatted name of the type
val sb : System.Text.StringBuilder
namespace System.Text
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
System.Text.StringBuilder() : unit
System.Text.StringBuilder(capacity: int) : unit
System.Text.StringBuilder(value: string) : unit
System.Text.StringBuilder(value: string, capacity: int) : unit
System.Text.StringBuilder(capacity: int, maxCapacity: int) : unit
System.Text.StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
val build : (System.Type -> unit)
property System.Type.IsGenericType: bool
val tick : int
property System.Reflection.MemberInfo.Name: string
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
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
module Printf

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

Full name: Microsoft.FSharp.Core.Printf.bprintf
val args : System.Type []
System.Type.GetGenericArguments() : System.Type []
val i : int32
property System.Array.Length: int
System.Text.StringBuilder.ToString() : string
System.Text.StringBuilder.ToString(startIndex: int, length: int) : string
Raw view Test code New version

More information

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