C#的'动态'在F# [英] C#'s 'dynamic' in F#
问题描述
在C#中使用DLR的一个例子如下:
One example of using the DLR in C# is as follows:
dynamic dyn = new MyObject();
dyn.MyMethod(); //resolved at runtime
F#中的等价物是什么?
what would be the equivalent in F#?
谢谢。
推荐答案
?
对C#中的动态
关键字具有类似的表达力(但它只能用于读取属性,方法调用和属性设置)。
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).
没有内置的实现,允许您动态地使用.NET类的属性或方法(通过Reflection或DLR),但是社区中有一些相当可靠的实现。这已在之前的另一个SO问题中进行了讨论。
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.
还有一些?
的实现,可以访问一些常见的数据源,如SQL数据库。例如,此MSDN文章包含一个允许您写入的定义 db?Query?Foo(1)
调用名为 Foo
的存储过程
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
.
对于各种其他类型(例如在XAML中查找元素或访问XML文档中的元素或属性),?
的定义非常容易写。
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.
这篇关于C#的'动态'在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!