C#3.5 DLR防爆pression.Dynamic问题 [英] C# 3.5 DLR Expression.Dynamic Question

查看:204
本文介绍了C#3.5 DLR防爆pression.Dynamic问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个小脚本语言和我试图将它移植到DLR所以,这是一个比较容易管理。到目前为止,它已经相当直截了当。我虽然已经尝试动态调用一个成员变量遇到了问题。当前语言运行在.NET和使用解析循环和反思要做到这一点,但我希望能摆脱这一点。这里是脚本语言的一个示例:

I have inherited a small scripting language and I am attempting to port it to the DLR so that it is a little easier to manage. So far it has been fairly straight forward. I have run into a problem though attempting to dynamically call members of a variable. The current language runs on .NET and uses a parsing loop and reflection to do this, but I was hoping to get away from that. Here is an example of the script language:

string $system1RemoteUri;
string $dbconnection = $config.GetDBConnection ("somedb");
float $minBad = 0.998;
float $minGood = 0.2;

$ systen1RemoteURI,$ minBad,和$名古是将要在该脚本中设置,随着$ DBConnection的变量。然而$ DBConnection的将得到在名为$配置通过一个变量的值。 4变量需要提供给调用者,所以他们被传递到拉姆达,最初作为空。这里是产生LAMBDA IL(调试视图):

$systen1RemoteURI, $minBad, and $minGood are variables that will be set in the script, along with $dbconnection. However $dbconnection will get its value from a variable passed in called $config. The 4 variables need to be available to the caller, so they are passed into the lambda, initially as null. Here is the generated Lambda IL (debug view):

.Lambda #Lambda1<Delegate6$1>(
    System.String& $$system1RemoteUri,
    System.String& $$dbconnection,
    System.Double& $$minBad,
    System.Double& $$minGood
    System.Object $$config) {
    .Block() {
        $$minBad = 0.998D;
        $$minGood = 0.2D
    }

    //Some assignment similar to...
    //.Dynamic Call GetDBConnection($config, "somedb");
}

我试图找出是如何使用防爆pression.Dynamic发出的$ config.GetDBConnection(somedb)。通过观察在Sympl库,我相信所发出的IL应该是这样的例子: 。动态调用GetdbConnection($配置,somedb),但我无法弄清楚如何实际发出,从防爆pression.Dynamic。

What I am trying to figure out is how to use Expression.Dynamic to emit the $config.GetDBConnection("somedb"). From looking at examples in the Sympl libraries I believe the emitted IL should look like: .Dynamic Call GetdbConnection($config, "somedb") but I cant figure out how to actually emit that from Expression.Dynamic.

它似乎想我不能正确地创建一个CallSiteBinder,我不明白什么参数的顺序是防爆pression.Dynamic,因为它似乎只想要成员被调用,而不是该基地。

It seems to want a CallSiteBinder which I cannot create correctly, and I do not understand what the order of parameters is to Expression.Dynamic, as it seems to only want the "member" being invoked, and not the base.

我不知道$ a的运行时类型的配置也只是一些对象,它实现了一个名为GetDBConnection(串)的功能。这是不提供的一个接口或基类。

I do not know the runtime type of $config it is just some object which implements a function called GetDBConnection(string). This is not provided by an interface or base class.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

您可以把它变成一个InvokeMemberBinder或将$ config.GetDBConnection成GetMember,然后做一个调用上,传递$ someDb作为结果的说法。

You can either turn this into an InvokeMemberBinder or turn "$config.GetDBConnection" into a GetMember and then do an Invoke on the result of that passing $someDb as the argument.

要实现你的GetMemberBinder和InvokeMemberBinder可以使用DLR外层DefaultBinder类。在最新的IronPython / IronRuby的源$ C ​​$ C你可以创建一个新的DefaultBinder实例无中生有。然后在你的FallbackGetMember / FallbackInvoke你可以调用defaultBinder.GetMember(...)和defaultBinder.Call(应改名调用)。这将解决大多数.NET类型适合你。同时它们实现IDynamicMetaObjectProvider所有对象将与它的工作为好。对于其他的动态操作,你可以使用默认的粘结剂的其他方法。如果你想开始自定义您的重载决议和具有约束力的规则,它有很多的旋钮可以转动的。

To implement your GetMemberBinder and InvokeMemberBinder you can use the DLR outer-layer DefaultBinder class. In the latest IronPython/IronRuby source code you can just create a new DefaultBinder instance out of thin air. Then in your FallbackGetMember / FallbackInvoke you can call defaultBinder.GetMember(...) and defaultBinder.Call (which should be renamed Invoke). That'll deal with most .NET types for you. Also all objects which implement IDynamicMetaObjectProvider will work with it as well. For other dynamic operations you can use the other methods on the default binder. And if you want to start customizing your overload resolution and binding rules it has lots of knobs you can turn.

不幸的是,默认的粘合剂不具有InvokeMemberBinder实现,现在所以你可能会更好过W / GetMember / Invoke的。

Unfortunately the default binder doesn't have an InvokeMemberBinder implementation right now so you're probably better off w/ GetMember/Invoke.

这篇关于C#3.5 DLR防爆pression.Dynamic问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆