动态对象和 WCF 支持

发布于 2024-08-29 07:44:40 字数 1174 浏览 3 评论 0原文

我想知道是否有人有幸获得 DynamicObject 序列化并与 WCF 一起使用?

这是我的小测试:

[DataContract]
class MyDynamicObject : DynamicObject
{
    [DataMember]
    private Dictionary<string, object> _attributes =
       new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string key = binder.Name;

        result = null;

        if (_attributes.ContainsKey(key))
            result = _attributes[key];

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _attributes.Add(binder.Name, value);

        return true;
    }
}

var dy = new MyDynamicObject();
var ser = new DataContractSerializer(typeof(MyDynamicObject));
var mem = new MemoryStream();
ser.WriteObject(mem, dy);

我得到的错误是:

System.Runtime.Serialization.InvalidDataContractException 未处理 Message=Type 'ElasticTest1.MyDynamicObject' 无法从未使用 DataContractAttribute 或 SerializedAttribute 标记的类型继承。考虑使用 DataContractAttribute 或 SerializedAttribute 标记基本类型“System.Dynamic.DynamicObject”,或从派生类型中删除它们。

有什么建议吗?

I was wondering if anyone has had any luck getting a DynamicObject to serialize and work with WCF?

Here’s my little test:

[DataContract]
class MyDynamicObject : DynamicObject
{
    [DataMember]
    private Dictionary<string, object> _attributes =
       new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string key = binder.Name;

        result = null;

        if (_attributes.ContainsKey(key))
            result = _attributes[key];

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _attributes.Add(binder.Name, value);

        return true;
    }
}

var dy = new MyDynamicObject();
var ser = new DataContractSerializer(typeof(MyDynamicObject));
var mem = new MemoryStream();
ser.WriteObject(mem, dy);

The error I get is:

System.Runtime.Serialization.InvalidDataContractException was unhandled
Message=Type 'ElasticTest1.MyDynamicObject' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'System.Dynamic.DynamicObject' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

Any suggestions?

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

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

发布评论

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

评论(2

只有一腔孤勇 2024-09-05 07:44:40

问题的解决方案

实施自定义IDynamicMetaObjectProvider

Solution for your problem

Implement custom IDynamicMetaObjectProvider

烏雲後面有陽光 2024-09-05 07:44:40

您可以使用诸如 Dictionary 之类的东西来实现这一点吗?

我正在尝试解决类似的问题。我的问题是我有 DTO 在客户端和服务器之间传输数据。但是,您应该始终拥有细粒度且扁平化的 DTO。

例如,如果客户端想要获取客户的名称和 ID,并且对其他任何内容都不感兴趣,那么理想情况下,您应该创建一个仅包含这两个属性的 DTO。如果您要为所有方法传输相同的 CustomerDTO,则会产生很多性能影响。您可能会传输大量冗余字段。

Can you use something like Dictionary<TKey, TValue> to achieve this?

I am trying to solve a similar problem. My issue is that I have DTO's to transfer data between client and server. However, you should always have DTO's that are fine grained and flattened.

For example, if a client wants to get Customer's Name and ID and it is not interested in anything else, ideally, you should create a DTO that only has these 2 properties in it. If you were to transfer the same CustomerDTO for all methods, there is a lot of performance implications. You could be transferring a lot of redundant fields.

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