简体   繁体   English

F#动态object接入

[英]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?有没有办法访问 F# 中类似于 C# 动态的 DLR object(例如 DynamicObject 子类实例)成员(属性和方法)?

There is a module now on nuget that uses the dlr to implement the dynamic operator.现在 nuget 上有一个模块,它使用 dlr 来实现动态运算符。 FSharp.Interop.Dynamic 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性能它使用Dynamitey进行 dlr 调用,实现缓存并且是一个 PCL 库
  • Handles methods that return void, you'll get a binding exception if you don't discard results of those.处理返回 void 的方法,如果您不丢弃这些结果,您将获得绑定异常。
  • 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 dlr 自动处理通过 function 调用委托返回的情况,这也将允许您对 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 .它是开源的,Apache 许可证,您可以查看实现和基本单元测试示例案例

As eriawan mentioned, the ?正如 eriawan 提到的, ? operator behaves a bit like the dynamic type in C#.运算符的行为有点像 C# 中的dynamic类型。 The article about calling SQL doesn't rely on anything from the DLR, because you can provide your own implementation of the ?关于调用 SQL 的文章不依赖于 DLR 的任何内容,因为您可以提供自己的? operator and the compiler uses it directly.运算符和编译器直接使用它。

I also wrote a brief example of how to use the ?我还写了一个简短的例子来说明如何使用? operator to call members using DLR, which is available on F# snippets and there is a more sophisticated version by Matthew Podwysocki .运营商使用 DLR 呼叫成员,可在 F# 片段上找到,Matthew Podwysocki 提供了更复杂的版本 Another snippet shows how to use it to call standard .NET types using Reflection .另一个片段显示了如何使用它来使用 Reflection 调用标准 .NET 类型

See also:也可以看看:

Yes, it is.是的。 You can use ?你可以用? operator in F#, and it will perform the same way in dynamic typing in C# and VB.NET in .NET 4.0. F# 中的运算符,它将在 C# 和 VB.NET 中的 C# 和 Z303CB0EF9EDB9082D64BBE0.55082D64BB9 中的 VB.NET 中执行相同的方式。 For a start, you can read this sample Dynamic SQLDataReader from Tomas Petricek's blog:首先,您可以从 Tomas Petricek 的博客中阅读此示例 Dynamic SQLDataReader:

http://tomasp.net/blog/dynamic-sql.aspx http://tomasp.net/blog/dynamic-sql.aspx

Here's a quote from his article:以下是他文章中的一段话:

In this article, we'll look how to use the dynamic operator to make the experience of using ADO.NET from F# dramatically better.在本文中,我们将了解如何使用动态运算符来显着改善使用 F# 中的 ADO.NET 的体验。 Dynamic operator (there are actually two of them) are a simple way of supporting dynamic invoke in F#.动态运算符(实际上有两个)是 F# 中支持动态调用的一种简单方法。 We can use it to write code that looks almost like an ordinary method call or property access, but is resolved dynamically at runtime (using the name of the method or property).我们可以使用它来编写看起来几乎像普通方法调用或属性访问但在运行时动态解析的代码(使用方法或属性的名称)。 The following example shows what we'll be able to write at the end of this article:下面的例子展示了我们在本文结尾可以写的内容:

 // Call 'GetProducts' procedure with 'CategoryID' set to 1 use conn = new DynamicSqlConnection(connectionString) use cmd = conn?GetProducts cmd?CategoryID <- 1 conn.Open() // Read all products and print their names use reader = cmd.ExecuteReader() while reader.Read() do printfn "Product: %s" reader?ProductName

If you ever tried to call a SQL stored procedure directly using the SqlCommand, then you can surely appreciate the elegance of this code snippet.如果您曾经尝试使用 SqlCommand 直接调用 SQL 存储过程,那么您一定会欣赏到这段代码的优雅。 Let's now take a look at a larger example and some of the neat tricks that make this possible...现在让我们看一个更大的例子和一些使这成为可能的巧妙技巧......

And for more info, you can read the rest of his article.有关更多信息,您可以阅读他的文章中的 rest。 Happy dynamic coding in F#:) F#中的快乐动态编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM