Created
March 6, 2010 01:10
-
-
Save banshee/323402 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.IO | |
open System.Reflection | |
let rec extractDll opts = | |
match opts with | |
| "-d"::s::t -> Some s | |
| h::t -> extractDll t | |
| [] -> None | |
let dllName = | |
match extractDll (System.Environment.GetCommandLineArgs() |> List.ofSeq) with | |
| Some s -> s | |
| None -> System.Console.WriteLine "must provide -d dllFileName option"; exit 0 | |
let resolve _ (y: ResolveEventArgs) : Assembly = | |
try | |
Assembly.ReflectionOnlyLoad(y.Name) |> ignore | |
with | |
| :? System.IO.FileNotFoundException as xy -> printfn "got exception %A" xy | |
null | |
let domain = AppDomain.CurrentDomain | |
domain.add_ReflectionOnlyAssemblyResolve (ResolveEventHandler resolve) | |
let r = Assembly.ReflectionOnlyLoadFrom dllName | |
let types = r.GetExportedTypes () | |
let parametersToArgStrings (ps: ParameterInfo seq) = | |
let fn (p: ParameterInfo) = | |
match p.ParameterType.IsGenericParameter with | |
| true -> p.Name + ": " + "'" + p.ParameterType.ToString() | |
| false -> p.Name + ": " + p.ParameterType.ToString() | |
let vals = ps |> Seq.map fn |> Seq.map (fun f -> "(" + f + ")") | |
String.Join(" ", vals) | |
let parametersToGenerics (ps: ParameterInfo seq) = | |
let fn (p: ParameterInfo) = | |
match p.ParameterType.IsGenericParameter with | |
| true -> Some ("'" + p.Name) | |
| false -> None | |
let vals = ps |> Seq.choose fn | |
let result = String.Join(", ", vals) | |
if String.length result > 0 | |
then "<" + result + ">" | |
else "" | |
let parametersToCallStrings (ps: ParameterInfo seq) = | |
String.Join(", ", ps |> Seq.map (fun p -> p.Name)) | |
let printParameters (ps: ParameterInfo seq) t m = | |
ps |> Seq.iter (fun p -> printfn "type: %A; method: %A; paramter: %A" t m p) | |
let printMethod t (m: MethodInfo) = | |
let ps = (m.GetParameters()) | |
let argsParametersString = parametersToArgStrings ps | |
let callParametersString = parametersToCallStrings ps | |
let genericsString = parametersToGenerics ps | |
match m.IsStatic with | |
| false -> printfn " let %sFn%s (obj: %A) %s = obj.%s(%s)" m.Name genericsString t argsParametersString m.Name callParametersString | |
| true -> () | |
let printMemberInfo t (m: MemberInfo) = | |
match m with | |
| :? System.Reflection.MethodInfo as mi -> printMethod t mi | |
| _ -> printfn "// doing nothing with %A" m | |
let printMemberInfos (ms: MemberInfo seq) t = | |
ms |> Seq.iter (printMemberInfo t) | |
let printTypes (ts: Type seq) = | |
let toModuleName (t: Type) = | |
t.FullName.Replace(".", "_").Replace("`","_").Replace('+', '_') | |
ts |> Seq.iter (fun t -> printfn ""; printfn "module %sFn = " (toModuleName t); printMemberInfos (t.GetMembers()) t) | |
printfn " let HackHackHackMakeSureTheresAtLeastOneFunctionHere() = ()" | |
printfn "namespace com.restphone" | |
printTypes types |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment