.Net 3.5 DLR DynamicObject 帮助

发布于 2024-08-22 13:35:52 字数 1489 浏览 2 评论 0原文

我一直在努力让一个简单的 DynamicObject 示例在 .NET 3.5 中工作。

使用 Codeplex 之外的最新版本的 DLR,我无法弄清楚相当于以下内容的 .NET 3.5 是什么:

public class DynamicObjectBag : DynamicObject
{
    private Dictionary<string, object> _properties = new Dictionary<string, object>();
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _properties.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _properties[binder.Name] = value;
        return true;
    }
}

dynamic foo = new DynamicObjectBag();
foo.Val1 = 3;
foo.Val2 = “Value 2”;

这当然是一个简化的示例。我计划从 DynamicObject 派生类,以便能够使用直接对象属性和使用相同语义点样式表示法或访问方法存储在字典中的属性。目标是让 DLR 兼容对象在 DLR 支持的语言中使用,并在应用程序升级到 .NET 4.0 时提供与 .NET 4.0 DLR 功能的未来兼容性。

我的问题是,在 .NET 4.0 之前,我没有与动态关键字等效的概念。 TryGetMember 等方法具有类似于 GetMemberBinder 的绑定器参数。现在 .NET 4.0 有一个 C# 默认绑定器可用,它将允许在使用动态关键字时发生绑定,但是我一直无法找到或确定如何在 .NET 3.5 中执行等效操作。

目前,我的理解是,我需要编写一个自定义绑定程序,它基本上会复制 .NET 4.0 默认 C# 绑定程序中可用的逻辑类型。

请有人指出我如何在 .NET 3.5 中使用 DynamicObject 并在运行时添加属性等的正确方向,而无需访问动态关键字。

参考资料:
向动态对象动态添加成员
http://tomlev2 .wordpress.com/2009/10/08/c-4-0-implementing-a-custom-dynamic-object/

I have been struggling to get a simple DynamicObject example working in .NET 3.5.

Using the latest build of the DLR off codeplex I haven’t been able to figure out what the .NET 3.5 equivalent to the following is:

public class DynamicObjectBag : DynamicObject
{
    private Dictionary<string, object> _properties = new Dictionary<string, object>();
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _properties.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _properties[binder.Name] = value;
        return true;
    }
}

dynamic foo = new DynamicObjectBag();
foo.Val1 = 3;
foo.Val2 = “Value 2”;

This is of course a simplified example. I plan to derive classes from DynamicObject so I am able to use both direct object properties and properties stored in the dictionary using the same semantic dot style notation or access methods. The goal is to have DLR compatible objects for use in DLR supported languages and provide for future compatibility with .NET 4.0 DLR capabilities when the application can be upgraded to .NET 4.0.

My problem is that pre-.NET 4.0 I have no equivalent concept to the dynamic keyword. The methods such as TryGetMember have a binder parameter like GetMemberBinder. Now .NET 4.0 has a C# default binder available which will allow binding to happen when using the dynamic keyword, however I have been unable to find or determine how to perform the equivalent in .NET 3.5.

At the moment it is my understanding that I would need to write a custom binder which would basically duplicate the type of logic available in the .NET 4.0 default C# binder.

Please someone point me in the right direction on how I can use a DynamicObject in .NET 3.5 and add properties etc. at runtime without access to the dynamic keyword.

References:
Dynamically adding members to a dynamic object
http://tomlev2.wordpress.com/2009/10/08/c-4-0-implementing-a-custom-dynamic-object/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亚希 2024-08-29 13:35:52

您将需要创建一个自定义绑定器,然后您可能希望使用 CallSite 来使用该绑定器,以便缓存操作。

您可以使用外层中的 DefaultBinder 来完成大部分工作 - 您只需要创建一个,然后它具有像 GetMember 这样的方法来执行绑定。然后,您的绑定器的实现只是使用 DefaultBinder 并应用任何规则来装箱返回的值类型(DLR 要求所有返回类型都是对象)。

You will need to create a custom binder and you will probably then want to consume the binder using a CallSite so that the actions are cached.

You can use the DefaultBinder in the outer layer to do most of this - you just need to create one and then it has methods like GetMember on it to do the binding. The implementation of your binder then is just using the DefaultBinder and applying any rules for boxing value types that come back (the DLR requires all return types to be object).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文