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)
let sourceNames =
<@ List.mapi (fun i j -> i+j) [1;2;3] @>
|> (function Call(_,mi,_) -> mi.DeclaringType |> moduleSourceName, mi |> methodSourceName);