How to get the name of the F # module, function, etc. From the quotation expression

I continue to work on a printer for F # expressions, it should not be ideal, but I would like to see what is possible. Active patterns in Microsoft.FSharp.Quotations.Patternsand Microsoft.FSharp.Quotations.DerivedPatternsused to decompose quoted expressions usually provide instances MemberInfowhen necessary, they can be used to get the name of a property, function, etc. And their type of "declaration", for example as a module or a static class. The problem is that I only know how to get CompiledNamefrom these instances, but I would like the name F #. For instance,

> <@ List.mapi (fun i j -> i+j) [1;2;3] @> |> (function Call(_,mi,_) -> mi.DeclaringType.Name, mi.Name);;
val it : string * string = ("ListModule", "MapIndexed")

How can this correspondence be rewritten for a refund ("List", "mapi")? Is it possible?

FYI, here is my final polished solution from Stringer Bell and pblasucci help:

let moduleSourceName (declaringType:Type) =
    FSharpEntity.FromType(declaringType).DisplayName

let methodSourceName (mi:MemberInfo) =
    mi.GetCustomAttributes(true)
    |> Array.tryPick 
            (function
                | :? CompilationSourceNameAttribute as csna -> Some(csna)
                | _ -> None)
    |> (function | Some(csna) -> csna.SourceName | None -> mi.Name)

//usage:
let sourceNames =
    <@ List.mapi (fun i j -> i+j) [1;2;3] @> 
    |> (function Call(_,mi,_) -> mi.DeclaringType |> moduleSourceName, mi |> methodSourceName);
+5
1

Powerpack F # :

open Microsoft.FSharp.Metadata
...
| Call(_, mi, _) ->
    let ty = Microsoft.FSharp.Metadata.FSharpEntity.FromType(mi.DeclaringType)
    let name = ty.DisplayName // name is List

, powerpack.

Edit:

pblasucci, CompilationSourceName :

let infos = mi.DeclaringType.GetMember(mi.Name)
let att = infos.[0].GetCustomAttributes(true)
let fName =
    (att.[1] :?> CompilationSourceNameAttribute).SourceName // fName is mapi 
+4

All Articles