Search code examples
c#.netf#dynamic-language-runtime

F# dynamic object access


Is there a way to access DLR object (eg. DynamicObject subclass instance) members (properties and methods) in F# that is similar to C# dynamic ?


Solution

  • There is a module now on nuget that uses the dlr to implement the dynamic operator. FSharp.Interop.Dynamic

    It has several advantages over a lot of the snippets out there.

    • Performance it uses Dynamitey for the dlr call which implements caching and is a PCL library
    • Handles methods that return void, you'll get a binding exception if you don't discard results of those.
    • The dlr handles the case of calling a delegate return by a function automatically, this will also allow you to do the same with an FSharpFunc
    • Adds an !? prefix operator to handle invoking directly dynamic objects and functions you don't have the type at runtime.

      It's open source, Apache license, you can look at the implementation and the basic unit test example cases.