|
| 1 | +namespace FSharp.Plotly |
| 2 | + |
| 3 | +open System |
| 4 | + |
| 5 | + |
| 6 | +module internal ApplyHelper = |
| 7 | + |
| 8 | + open System.Reflection |
| 9 | + |
| 10 | + /// Creates an instance of the Object according to applyStyle and applies the function.. |
| 11 | + let buildApply (applyStyle:'a -> 'a) = |
| 12 | + let instance = |
| 13 | + System.Activator.CreateInstance<'a>() |
| 14 | + applyStyle instance |
| 15 | + |
| 16 | + /// Applies 'applyStyle' to item option. If None it creates a new instance. |
| 17 | + let optBuildApply (applyStyle:'a -> 'a) (item:'a option) = |
| 18 | + match item with |
| 19 | + | Some item' -> applyStyle item' |
| 20 | + | None -> buildApply applyStyle |
| 21 | + |
| 22 | + /// Applies Some 'applyStyle' to item. If None it returns 'item' unchanged. |
| 23 | + let optApply (applyStyle:('a -> 'a) option) (item:'a ) = |
| 24 | + match applyStyle with |
| 25 | + | Some apply -> apply item |
| 26 | + | None -> item |
| 27 | + |
| 28 | + /// Returns the proptery name from quotation expression |
| 29 | + let tryGetPropertyName (expr : Microsoft.FSharp.Quotations.Expr) = |
| 30 | + match expr with |
| 31 | + | Microsoft.FSharp.Quotations.Patterns.PropertyGet (_,pInfo,_) -> Some pInfo.Name |
| 32 | + | _ -> None |
| 33 | + |
| 34 | + /// Sets property value using reflection |
| 35 | + let trySetPropertyValue (o:obj) (propName:string) (value:obj) = |
| 36 | + let property = o.GetType().GetProperty(propName) |
| 37 | + try |
| 38 | + property.SetValue(o, value, null) |
| 39 | + Some o |
| 40 | + with |
| 41 | + | :? System.ArgumentException -> None |
| 42 | + | :? System.NullReferenceException -> None |
| 43 | + |
| 44 | + /// Gets property value as option using reflection |
| 45 | + let tryGetPropertyValue (o:obj) (propName:string) = |
| 46 | + try |
| 47 | + Some (o.GetType().GetProperty(propName).GetValue(o, null)) |
| 48 | + with |
| 49 | + | :? System.Reflection.TargetInvocationException -> None |
| 50 | + | :? System.NullReferenceException -> None |
| 51 | + |
| 52 | + /// Gets property value as 'a option using reflection. Cast to 'a |
| 53 | + let tryGetPropertyValueAs<'a> (o:obj) (propName:string) = |
| 54 | + try |
| 55 | + let v = (o.GetType().GetProperty(propName).GetValue(o, null)) |
| 56 | + Some (v :?> 'a) |
| 57 | + with |
| 58 | + | :? System.Reflection.TargetInvocationException -> None |
| 59 | + | :? System.NullReferenceException -> None |
| 60 | + |
| 61 | + /// Updates property value by given function |
| 62 | + let tryUpdatePropertyValueFromName (o:obj) (propName:string) (f: 'a -> 'a) = |
| 63 | + let v = optBuildApply f (tryGetPropertyValueAs<'a> o propName) |
| 64 | + trySetPropertyValue o propName v |
| 65 | + //o |
| 66 | + |
| 67 | + /// Updates property value by given function |
| 68 | + let tryUpdatePropertyValue (o:obj) (expr : Microsoft.FSharp.Quotations.Expr) (f: 'a -> 'a) = |
| 69 | + let propName = tryGetPropertyName expr |
| 70 | + let v = optBuildApply f (tryGetPropertyValueAs<'a> o propName.Value) |
| 71 | + trySetPropertyValue o propName.Value v |
| 72 | + //o |
| 73 | + |
| 74 | + let updatePropertyValueAndIgnore (o:obj) (expr : Microsoft.FSharp.Quotations.Expr) (f: 'a -> 'a) = |
| 75 | + tryUpdatePropertyValue o expr f |> ignore |
| 76 | + |
0 commit comments