English 中文(简体)
A. 寻找强有力的一般机会 动态执行
原标题:Looking for robust, general op_Dynamic implementation
  • 时间:2011-02-20 14:41:08
  •  标签:
  • f#

我无法找到一个强有力的一般机会。 动态执行:谁能向我说一句话? 到目前为止,搜索只是转向玩具或特定目的的实施,但我要亲身对C#的静态动态实施(即处理抽签/所有案件,进行切身反响)作一比较(这是自我看C#静态动态以来的一段时期,因此,如果我关于其能力的说法不实,我会宽恕我)。

感谢!

最佳回答

有一个单元:FSharp.Interop.Dynamic,内容是关于核心的,应当利用dlr有力处理动态经营者。

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 .NET Standard 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
  • Add! 预设操作员处理直接援引动态物体和功能的问题,在操作时间,你没有这种类型。

    开放源,阿帕奇许可证,你可以查阅lement/a>,其中包括单位测试

问题回答

您永远不能完全全面执行<代码>?。 经营人可以不同类型地实施,视下列类型而定,可能需要做一些特殊的事情:

  • For Dictionary<T, R>, you d want it to use the lookup function of the dictionary
  • For the SQL objects in my article you referenced, you want it to use specific SQL API
  • For unknown .NET objects, you want it to use .NET Reflection

如果你重新寻找使用反思的实施工作,那么你可以使用在F#中实施的对莫诺迪奥·迪奥普具有约束力的一例(上查阅。 它相当完整,处理财产存取、使用方法以及固定成员的问题。 (其他链接档案大量使用F#汇编器内部成员。) 它直接使用反省,因此相当缓慢,但相当具体。

另一种选择是将经营者置于顶端。 NET 4.0 动态语言运行时间(因此,它将在C#4中使用与dynamic相同的对应文本)。 我不认为在其中某个地方执行这一规定,但这里是你如何能够做到的简单例子:

#r "Microsoft.CSharp.dll"
open System
open System.Runtime.CompilerServices
open Microsoft.CSharp.RuntimeBinder

let (?) (inst:obj) name (arg: T) :  R =
  // Create site (representing dynamic operation for converting result to  R 
  let convertSite = 
    CallSite<Func<CallSite, Object,  R>>.Create // 
      (Binder.Convert(CSharpBinderFlags.None, typeof< R>, null)) // 
  // Create site for the method call with single argument of type  T
  let callSite = 
    CallSite<Func<CallSite, Object,  T, Object>>.Create // 
      (Binder.InvokeMember
        ( CSharpBinderFlags.None, name, null, null, 
          [| CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
             CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) |]))
  // Run the method and perform conversion
  convertSite.Target.Invoke
    (convertSite, callSite.Target.Invoke(callSite, inst, arg))

let o = box (new Random())
let a : int = o?Next(10)

这种方法只能以单一论点为根据。 (通过研究C#汇编者为<代码>dynamic <>/code>在职业中产生的代码,可以发现如何做到这一点。) 我猜测,如果你(从头一开始)与使用德国航天中心(在第二种情况下)的做法混在一起,你会得到最有力的执行。

EDIT: I also posted the code to F# Snippets. Here is the version using DLR: http://fssnip.net/2U and here is the version from F# plugin (using .NET Reflection): http://fssnip.net/2V





相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签