Advertisement
Guest User

Simple AssemblyLoader

a guest
Jul 20th, 2016
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.28 KB | None | 0 0
  1. namespace Monitor
  2. module AssemblyLoader =
  3.     open Newtonsoft.Json
  4.     open System.IO
  5.     open System.Timers
  6.     open System.Reflection
  7.     open System.Collections.Generic
  8.  
  9.     type AssemblyFile = {path:string; operations:Operation[]}
  10.     and Operation = {kind:string; name:string; exposedName:string; operations:Operation[]; refresh:float; arguments:Argument[];}
  11.     and Argument = {kind:string; value:string;}
  12.  
  13.     type AssemblyRunner(file:AssemblyFile) as this =
  14.         do
  15.             file.operations |>
  16.                 Array.iter(fun operation ->
  17.                     let timer = new Timer()
  18.                     timer.Elapsed.Add(fun args -> this.RunOperation operation)
  19.                     timer.Interval <- operation.refresh
  20.                     timer.Enabled <- true
  21.                     timer.Start())
  22.        
  23.         member this.File = file
  24.         member this.Exposed = new Dictionary<string, System.Object>()
  25.  
  26.         member this.SetExposedValue (key:string) (item:obj) =
  27.             match item with
  28.             | null -> () //System.Console.WriteLine("Called " + key)
  29.             | _ ->
  30.                 this.Exposed.Add(key, item) |> ignore
  31.                 printfn "%A" this.Exposed.Count
  32.                 printfn "Setting %s to %s" key (item.ToString())
  33.  
  34.         member this.RunOperation (operation:Operation) =
  35.             let assembly = Assembly.LoadFile(this.File.path)
  36.             let instance, t =
  37.                 match operation.kind with
  38.                 | "class" -> (None, Some(assembly.GetType(operation.name)))
  39.                 | "instance class" ->
  40.                     let asm = assembly.GetType(operation.name)
  41.                     (Some(System.Activator.CreateInstance(asm)), Some(asm))
  42.                 | _ -> (None, None)
  43.                
  44.             match t with
  45.             | Some(clazz) ->
  46.                 operation.operations |> Array.iter(fun item -> this.RunParentedOperation assembly item clazz instance)
  47.             | _ -> ()
  48.  
  49.         member this.RunParentedOperation (assembly:Assembly) (operation:Operation) (parent:System.Type) (instance:Option<obj>) =
  50.             match (operation.kind, instance) with
  51.             | ("instance member", Some(inst)) ->
  52.                 let field = parent.GetField(operation.name)
  53.                 let value = field.GetValue(inst)
  54.                 this.SetExposedValue operation.exposedName value
  55.             | ("static member", _) ->
  56.                 let field = parent.GetField(operation.name)
  57.                 let value = field.GetValue(null)
  58.                 this.SetExposedValue operation.exposedName value
  59.             | ("instance method", Some(inst)) ->
  60.                 this.SetExposedValue operation.exposedName
  61.                     (parent.InvokeMember(operation.name, BindingFlags.InvokeMethod, null, inst, [||]))
  62.             | ("static method", _) ->
  63.                 this.SetExposedValue operation.exposedName
  64.                     (parent.InvokeMember(operation.name, BindingFlags.InvokeMethod, null, null, [||]))
  65.             | _ -> ()
  66.  
  67.  
  68.     let getAssemblyConfiguration (path:string) : AssemblyFile =
  69.         JsonConvert.DeserializeObject<AssemblyFile>(File.ReadAllText(path))
  70.    
  71.     let load (path:string) : AssemblyRunner =
  72.         let config = getAssemblyConfiguration path
  73.         new AssemblyRunner(config)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement