动态对象和 WCF 支持
我想知道是否有人有幸获得 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题的解决方案
实施自定义
IDynamicMetaObjectProvider
Solution for your problem
Implement custom
IDynamicMetaObjectProvider
您可以使用诸如
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.