Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.35 KB | None | 0 0
  1. open System
  2. open System.Diagnostics
  3. open System.Reflection
  4. open System.Text.RegularExpressions
  5.  
  6. let pwd = System.Environment.CurrentDirectory
  7. let getCurrentSourceFile =
  8.     let _base = String.concat "/" [pwd; "Testlib"]
  9.     in (_base+".cs", _base+".dll")
  10.  
  11. let getFileAsString filepath =
  12.     String.concat "\n" <| System.IO.File.ReadLines(filepath)
  13.  
  14. let writeToFile filepath contents =
  15.     if not <| System.IO.File.Exists filepath
  16.     then System.IO.File.WriteAllLines (filepath, contents)
  17.     else System.IO.File.WriteAllLines (filepath, contents)
  18.  
  19. let compileFile source target =
  20.   let process = Process.Start("mcs", "-t:library -o:"+target+" "+source)
  21.   process.WaitForExit()
  22.   printfn "%d" process.ExitCode
  23.   0
  24.  
  25. [<EntryPoint>]
  26. let main argv =
  27.     let (source, target) = getCurrentSourceFile
  28.     let current_source = getFileAsString source
  29.     let tmp_path = target + ".tmp"
  30.     let tmp_out = target + ".dll"
  31.     writeToFile tmp_path [|current_source|]
  32.    
  33.     compileFile tmp_path tmp_out
  34.    
  35.     let DLL = Assembly.LoadFile(tmp_out)
  36.     let Test = DLL.GetType("Test")
  37.     let Test' = Activator.CreateInstance(Test)
  38.    let Method = Test.GetMethod("add5")
  39.    let x = downcast (Method.Invoke (Test', [|5|])) : int
  40.    
  41.     printfn "%d" <| x // prints 10
  42.     0 // return an integer exit code
  43. ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement