13 people like it.

Find verbose .NET types using Reflection

Searches all (currently loaded) types using Reflection to find the types with longest and shortest names of members. Uses average length of all type member names as a metric.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
open System
open System.Reflection

// Find all types in all loaded assemblies
let allTypes = 
  seq { for a in AppDomain.CurrentDomain.GetAssemblies() do
          for t in a.GetTypes() do
            if t.IsPublic then yield t }

// Get type name together with member verbosity
// (average length of a member name)
let verbose = 
  [| for t in allTypes do 
        let members = t.GetMembers()
        if members.Length > 0 then
          let length = members |> Seq.averageBy (fun m -> float m.Name.Length)
          yield t.Name, length |]

// Sort using the specified function 'f' and print top 'n'
let report f n = 
  let types = verbose |> Array.sortBy (snd >> f) |> Seq.take n 
  let index = ref 1
  for n, v in types do
    printfn "%d - %s (%f)" index.Value n v
    incr index

// Print the 10 least verbose types
report id 10
// Print the 10 most verbose types
report (~-) 10
namespace System
namespace System.Reflection
val allTypes : seq<Type>

Full name: Script.allTypes
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val a : Assembly
type AppDomain =
  inherit MarshalByRefObject
  member ActivationContext : ActivationContext
  member AppendPrivatePath : path:string -> unit
  member ApplicationIdentity : ApplicationIdentity
  member ApplicationTrust : ApplicationTrust
  member ApplyPolicy : assemblyName:string -> string
  member BaseDirectory : string
  member ClearPrivatePath : unit -> unit
  member ClearShadowCopyPath : unit -> unit
  member CreateComInstanceFrom : assemblyName:string * typeName:string -> ObjectHandle + 1 overload
  member CreateInstance : assemblyName:string * typeName:string -> ObjectHandle + 3 overloads
  ...

Full name: System.AppDomain
property AppDomain.CurrentDomain: AppDomain
AppDomain.GetAssemblies() : Assembly []
val t : Type
Assembly.GetTypes() : Type []
property Type.IsPublic: bool
val verbose : (string * float) []

Full name: Script.verbose
val members : MemberInfo []
Type.GetMembers() : MemberInfo []
Type.GetMembers(bindingAttr: BindingFlags) : MemberInfo []
property Array.Length: int
val length : float
module Seq

from Microsoft.FSharp.Collections
val averageBy : projection:('T -> 'U) -> source:seq<'T> -> 'U (requires member ( + ) and member DivideByInt and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.averageBy
val m : MemberInfo
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
property MemberInfo.Name: string
property String.Length: int
val report : f:(float -> 'a) -> n:int -> unit (requires comparison)

Full name: Script.report
val f : (float -> 'a) (requires comparison)
val n : int
val types : seq<string * float>
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val sortBy : projection:('T -> 'Key) -> array:'T [] -> 'T [] (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.sortBy
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val index : int ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val n : string
val v : float
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property Ref.Value: int
val incr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.incr
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id
Raw view Test code New version

More information

Link:http://fssnip.net/2p
Posted:13 years ago
Author:Tomas Petricek
Tags: fun , reflection , type