gpt4 book ai didi

c# - 动态对象和 WCF 支持

转载 作者:太空狗 更新时间:2023-10-29 17:44:02 25 4
gpt4 key购买 nike

我想知道是否有人有幸获得 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 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.

有什么建议吗?

最佳答案

Solution for your problem

实现自定义IDynamicMetaObjectProvider

关于c# - 动态对象和 WCF 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2671851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com