是否有一种类似于C#动态类型的方式可以访问F#中的DLR对象(例如DynamicObject子类实例)成员(属性和方法)?
是否有一种类似于C#动态类型的方式可以访问F#中的DLR对象(例如DynamicObject子类实例)成员(属性和方法)?
?
运算符的行为有点像C#中的dynamic
类型。关于调用SQL的文章不依赖于DLR中的任何内容,因为您可以提供自己的实现?
运算符,编译器直接使用它。?
运算符,并且在.NET 4.0中,它会以与C#和VB.NET动态类型相同的方式执行。首先,您可以阅读Tomas Petricek博客上的此示例Dynamic SQLDataReader:
http://tomasp.net/blog/dynamic-sql.aspx
以下是他文章中的一句引用:In this article, we'll look how to use the dynamic operator to make the experience of using ADO.NET from F# dramatically better. Dynamic operator (there are actually two of them) are a simple way of supporting dynamic invoke in 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. Let's now take a look at a larger example and some of the neat tricks that make this possible...
如果您需要更多信息,可以阅读他的文章的其余部分。 在F#中愉快地进行动态编码 :)