C#'s 'dynamic' in F#

前端 未结 4 709
梦如初夏
梦如初夏 2020-12-31 02:26

One example of using the DLR in C# is as follows:

dynamic dyn = new MyObject();
dyn.MyMethod(); //resolved at runtime
         


        
4条回答
  •  情歌与酒
    2020-12-31 03:15

    The ? operator has similar expressive power to the dynamic keyword in C# (but it can be only used for reading of properties, method invocation and setting of properties).

    There is no built-in implementation that would allow you to dynamically use properties or methods of a .NET class (via Reflection or DLR), but there are some fairly solid implementations from the community. This has been discussed in another SO question before.

    There are also implementations of ? that allow you access some common data sources such as SQL databases. For example, this MSDN article includes a definition that allows you to write db?Query?Foo(1) to call a stored procedure named Foo.

    For various other types (such as finding an element in XAML or accessing elements or attributes in XML document), the definition of ? is quite easy to write.

提交回复
热议问题