FSharp.Interop.Dynamic


Examples of using Dynamic operator and Functions

1: 
2: 
3: 
4: 
5: 
namespace Tests

open FSharp.Interop.Dynamic
open FSharp.Interop.Dynamic.Operators
open FSharp.Interop.Dynamic.SymbolicString

Call a method with a variable (ideally you wouldn't know it was as a string).

1: 
2: 
3: 
4: 
5: 
    [<Fact>]
    let ``Call method off of an object dynamically with variable`` ()=
       let method = "Substring"
       "HelloWorld"?(method)(0,5) 
            |> should equal "Hello"

Set a property with dlr, Expando only responds to the dlr.

1: 
2: 
3: 
4: 
5: 
    [<Fact>]
    let ``Test Expando Set and Get`` ()=
        let ex1 = ExpandoObject()
        ex1?Test<-"Hi";
        ex1?Test |> should equal "Hi"

!? will invoke without a name, dynamic function or the like. Dyn.namedArg allows you to wrap your arguments with names as part of the invocation.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
    [<Fact>]
    let ``Test NamedArgs`` ()=
        let buildObj = !?Build<ExpandoObject>.NewObject (
                                                            Dyn.namedArg "One" 1,
                                                            Dyn.namedArg "Two" 2
                                                        )
        buildObj?One |> should equal 1
        buildObj?Two |> should equal 2

Use the dlr to call the explict operator with a reflected type

1: 
2: 
3: 
4: 
    [<Fact>]
    let ``Test dynamic Explicit Conversion`` ()=
        let ele = XElement(XName.Get("Test"),"50")
        ele |> Dyn.explicitConvertTo typeof<Int32> |> should equal 50

Use the dlr to call the implict operato rwith a reflected type

1: 
2: 
3: 
4: 
5: 
    [<Fact>]
    let ``Test dynamic Implicit Conversion`` ()=
        let ele = 50
        let actual = ele |> Dyn.implicitConvertTo typeof<decimal>
        actual |> should equal 50M

Use the dlr to call the explict operator with inferred type from usage

1: 
2: 
3: 
4: 
5: 
    [<Fact>]
    let ``Test Explicit Conversion`` ()=
        let ele = XElement(XName.Get("Test"),"50")
        let elet:int = Dyn.explicitConvert ele
        elet |> should equal 50

Use the dlr to call the implicit operator with inferred type from usage

1: 
2: 
3: 
4: 
5: 
    [<Fact>]
    let ``Test Implicit Conversion`` ()=
        let ele = 50
        let actual:decimal = ele |> Dyn.implicitConvert
        actual |> should equal 50m

Use operators dynamically (better without knowing the types).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
    [<Fact>]
    let ``Basic Operator Op Tests`` ()=
        65 ?%? 10 |> should equal 5
        5 ?*? 4 |> should equal 20
        5 ?+? 4 |> should equal 9
        5 ?-? 3 |> should equal 2
        15 ?/? 5 |> should equal 3
        
        5 ?&&&? 3 |> should equal 1
        5 ?|||? 3 |> should equal 7
        5 ?^^^? 3 |> should equal 6
        23 ?<<<? 2 |> should equal 92
        (-105) ?>>>? 1 |> should equal (-53)
        
        10 ?<=? 5 |> should equal false
        5 ?<=? 10 |> should equal true
        10 ?<=? 10 |> should equal true
        
        10 ?>=? 5 |> should equal true
        5 ?>=? 10 |> should equal false
        10 ?>=? 10 |> should equal true
        
        10 ?<? 5 |> should equal false
        5 ?<? 10 |> should equal true
        10 ?<? 10 |> should equal false
        
        10 ?>? 5 |> should equal true
        5 ?>? 10 |> should equal false
        10 ?>? 10 |> should equal false
        
        10 ?<>? 5 |> should equal true
        10 ?<>? 10 |> should equal false
        
        10 ?=? 5 |> should equal false
        10 ?=? 10 |> should equal true
Multiple items
module Tests

from Tests

--------------------
namespace Tests
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Interop
namespace FSharp.Interop.Dynamic
module Operators

from FSharp.Interop.Dynamic
module SymbolicString

from FSharp.Interop.Dynamic
namespace System
namespace System.Dynamic
namespace System.Collections
namespace System.Collections.Generic
namespace System.Xml
namespace System.Xml.Linq
namespace System.Numerics
namespace Microsoft
namespace Microsoft.CSharp
namespace Microsoft.CSharp.RuntimeBinder
namespace System.Linq
namespace System.Linq.Expressions
Multiple items
type TestEvent =
  new : unit -> TestEvent
  member OnEvent : obj:'a * args:'b -> 'c
  member add_Event : obj -> obj
  member Event : obj
  member remove_Event : obj -> obj

--------------------
new : unit -> TestEvent
val event1 : obj
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
Multiple items
type CLIEventAttribute =
  inherit Attribute
  new : unit -> CLIEventAttribute

--------------------
new : unit -> CLIEventAttribute
val __ : TestEvent
Multiple items
val obj : 'a

--------------------
type obj = System.Object
val args : 'b
Multiple items
type TestFuncs =
  new : unit -> TestFuncs
  static member Plus3 : obj

--------------------
new : unit -> TestFuncs
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Multiple items
type DynamicOperatorMock =
  inherit DynamicObject
  new : unit -> DynamicOperatorMock
  override TryBinaryOperation : binder:BinaryOperationBinder * arg:obj * result:byref<obj> -> bool

--------------------
new : unit -> DynamicOperatorMock
Multiple items
type DynamicObject =
  member GetDynamicMemberNames : unit -> IEnumerable<string>
  member GetMetaObject : parameter:Expression -> DynamicMetaObject
  member TryBinaryOperation : binder:BinaryOperationBinder * arg:obj * result:obj -> bool
  member TryConvert : binder:ConvertBinder * result:obj -> bool
  member TryCreateInstance : binder:CreateInstanceBinder * args:obj[] * result:obj -> bool
  member TryDeleteIndex : binder:DeleteIndexBinder * indexes:obj[] -> bool
  member TryDeleteMember : binder:DeleteMemberBinder -> bool
  member TryGetIndex : binder:GetIndexBinder * indexes:obj[] * result:obj -> bool
  member TryGetMember : binder:GetMemberBinder * result:obj -> bool
  member TryInvoke : binder:InvokeBinder * args:obj[] * result:obj -> bool
  ...

--------------------
DynamicObject() : DynamicObject
val binder : BinaryOperationBinder
val arg : obj
val result : byref<obj>
property BinaryOperationBinder.Operation: ExpressionType
Multiple items
type DynamicWeirdFlakyIndexer =
  inherit DynamicObject
  new : unit -> DynamicWeirdFlakyIndexer
  override TryGetIndex : GetIndexBinder * indexes:obj [] * result:byref<obj> -> bool
  override TrySetIndex : SetIndexBinder * indexes:obj [] * value:obj -> bool

--------------------
new : unit -> DynamicWeirdFlakyIndexer
val stuff : Dictionary<(obj * obj),obj>
Multiple items
type Dictionary<'TKey,'TValue> =
  new : unit -> Dictionary<'TKey, 'TValue> + 7 overloads
  member Add : key:'TKey * value:'TValue -> unit
  member Clear : unit -> unit
  member Comparer : IEqualityComparer<'TKey>
  member ContainsKey : key:'TKey -> bool
  member ContainsValue : value:'TValue -> bool
  member Count : int
  member EnsureCapacity : capacity:int -> int
  member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  ...
  nested type Enumerator
  nested type KeyCollection
  nested type ValueCollection

--------------------
Dictionary() : Dictionary<'TKey,'TValue>
Dictionary(capacity: int) : Dictionary<'TKey,'TValue>
Dictionary(comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
type obj = System.Object
val indexes : obj []
val __ : DynamicWeirdFlakyIndexer
val value : obj
Dictionary.Add(key: obj * obj, value: obj) : unit
val ( Call method off of an object dynamically ) : unit -> 'a
val ( Call method off of an object dynamically with variable ) : unit -> 'a
val method : string
val ( Test Expando Set and Get ) : unit -> 'a
val ex1 : ExpandoObject
Multiple items
type ExpandoObject =
  new : unit -> ExpandoObject

--------------------
ExpandoObject() : ExpandoObject
val ( Test Direct Invoke ) : unit -> 'a
module Dyn

from FSharp.Interop.Dynamic
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
Multiple items
type Format<'Printer,'State,'Residue,'Result> = PrintfFormat<'Printer,'State,'Residue,'Result>

--------------------
type Format<'Printer,'State,'Residue,'Result,'Tuple> = PrintfFormat<'Printer,'State,'Residue,'Result,'Tuple>
val ( Test Void Method ) : unit -> 'a
Multiple items
val array : List<string>

--------------------
type 'T array = 'T []
Multiple items
type List<'T> =
  new : unit -> List<'T> + 2 overloads
  member Add : item:'T -> unit
  member AddRange : collection:IEnumerable<'T> -> unit
  member AsReadOnly : unit -> ReadOnlyCollection<'T>
  member BinarySearch : item:'T -> int + 2 overloads
  member Capacity : int with get, set
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
  member CopyTo : array:'T[] -> unit + 2 overloads
  ...
  nested type Enumerator

--------------------
List() : List<'T>
List(capacity: int) : List<'T>
List(collection: IEnumerable<'T>) : List<'T>
val ( Test SetAll ) : unit -> 'a
val e1 : ExpandoObject
val ( Test Lambda methods ) : unit -> 'a
val ex1 : obj
val x : int
val y : int
property TestFuncs.Plus3: obj
val ( Test FSharp Lambda Tuple arg ) : unit -> 'a
val dyn : obj
val z : int
val ( Test FSharp Lambda 2 arg ) : unit -> 'a
val x : obj
val ( Test FSharp Lambda 3 arg not tupled ) : unit -> 'a
val ( Test FSharp Lambda 4 arg ) : unit -> 'a
val bbq : int
val ( Test FSharp Lambda 5 arg ) : unit -> 'a
val unknownfunc : obj
val etc : int
val go : (int -> int -> int -> int -> int -> obj)
val ( Test Events ) : unit -> 'a
val pocoObj : TestEvent
val refBool : bool ref
Multiple items
val ref : value:'T -> 'T ref

--------------------
type 'T ref = Ref<'T>
val myevent : obj
val memberAddAssign : memberName:string -> value:obj -> target:obj -> unit
val memberSubtractAssign : memberName:string -> value:obj -> target:obj -> unit
val ( Test NamedArgs ) : unit -> 'a
val buildObj : obj
val ( Test dynamic Explicit Conversion ) : unit -> 'a
val ele : XElement
Multiple items
type XElement =
  inherit XContainer
  new : name:XName -> XElement + 4 overloads
  member AncestorsAndSelf : unit -> IEnumerable<XElement> + 1 overload
  member Attribute : name:XName -> XAttribute
  member Attributes : unit -> IEnumerable<XAttribute> + 1 overload
  member DescendantNodesAndSelf : unit -> IEnumerable<XNode>
  member DescendantsAndSelf : unit -> IEnumerable<XElement> + 1 overload
  member FirstAttribute : XAttribute
  member GetDefaultNamespace : unit -> XNamespace
  member GetNamespaceOfPrefix : prefix:string -> XNamespace
  member GetPrefixOfNamespace : ns:XNamespace -> string
  ...

--------------------
XElement(name: XName) : XElement
XElement(other: XElement) : XElement
XElement(other: XStreamingElement) : XElement
XElement(name: XName, content: obj) : XElement
XElement(name: XName, [<System.ParamArray>] content: obj []) : XElement
type XName =
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member LocalName : string
  member Namespace : XNamespace
  member NamespaceName : string
  member ToString : unit -> string
  static member Get : expandedName:string -> XName + 1 overload
XName.Get(expandedName: string) : XName
XName.Get(localName: string, namespaceName: string) : XName
val explicitConvertTo : convertType:System.Type -> target:obj -> 'TResult
val typeof<'T> : System.Type
val ( Test dynamic Implicit Conversion ) : unit -> 'a
val ele : int
val actual : obj
val implicitConvertTo : convertType:System.Type -> target:obj -> 'TResult
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)

--------------------
type decimal = System.Decimal

--------------------
type decimal<'Measure> = decimal
val ( Test Explicit Conversion ) : unit -> 'a
val elet : int
val explicitConvert : target:obj -> 'TResult
val ( Test Implicit Conversion ) : unit -> 'a
val actual : decimal
val implicitConvert : target:obj -> 'TResult
val ( Test Implicit Conversion Fail ) : unit -> 'a
val ignore : value:'T -> unit
Multiple items
type RuntimeBinderException =
  inherit Exception
  new : unit -> RuntimeBinderException + 2 overloads

--------------------
RuntimeBinderException() : RuntimeBinderException
RuntimeBinderException(message: string) : RuntimeBinderException
RuntimeBinderException(message: string, innerException: exn) : RuntimeBinderException
val ( Test Basic indexer ) : unit -> 'a
val archive : obj
val setIndexer : indexers:seq<'T> -> value:obj -> target:obj -> unit
val box : value:'T -> obj
val getIndexer : indexers:seq<'T> -> target:obj -> 'TResult
val ( Basic Operator Mock Tests ) : unit -> 'a
val left : obj
val dummy : obj
type ExpressionType =
  | Add = 0
  | AddChecked = 1
  | And = 2
  | AndAlso = 3
  | ArrayLength = 4
  | ArrayIndex = 5
  | Call = 6
  | Coalesce = 7
  | Conditional = 8
  | Constant = 9
  ...
field ExpressionType.Modulo: ExpressionType = 25
field ExpressionType.Multiply: ExpressionType = 26
field ExpressionType.Add: ExpressionType = 0
field ExpressionType.Subtract: ExpressionType = 42
field ExpressionType.Divide: ExpressionType = 12
field ExpressionType.And: ExpressionType = 2
field ExpressionType.Or: ExpressionType = 36
field ExpressionType.ExclusiveOr: ExpressionType = 14
field ExpressionType.LeftShift: ExpressionType = 19
field ExpressionType.RightShift: ExpressionType = 41
val ( Basic Operator Op Tests ) : unit -> 'a
Fork me on GitHub