默认情况下,对继承自DynamicObject的对象进行序列化操作是不会报错的,但是并没有实际序列化出任何东西来
为了让它进行序列化,我们改造一下实现类,实现IXmlSerializable接口

#region 字段
///<summary> /// 集合字典,存放实际的对象 ///</summary> private Dictionary<string, object> dictionary =new Dictionary<string, object>();
#endregion
#region 公有方法
publicoverridebool TryGetMember(GetMemberBinder binder, outobject result) { string name = binder.Name.ToLower(); bool exist = dictionary.TryGetValue(name, out result); if (!exist) { // 不存在,默认空 result =null; } returntrue; }
publicoverridebool TrySetMember(SetMemberBinder binder, object value) { if (value ==null) { // 删除这个属性 Remove(binder.Name.ToLower()); } else { dictionary[binder.Name.ToLower()] = value; } returntrue; }
#endregion #region IXmlSerializable 通过实现该接口,对DynamicObject进行XML序列化
public System.Xml.Schema.XmlSchema GetSchema() { returnnull; }
publicvoid ReadXml(System.Xml.XmlReader reader) { XmlSerializer keySerializer =new XmlSerializer(typeof(string)); XmlSerializer typeSerializer =new XmlSerializer(typeof(string)); if (reader.IsEmptyElement ||!reader.Read()) { return; }
while (reader.NodeType != XmlNodeType.EndElement) { reader.ReadStartElement("item");
reader.ReadStartElement("key"); string key = (string)keySerializer.Deserialize(reader); reader.ReadEndElement();
reader.ReadStartElement("type"); string type = (string)typeSerializer.Deserialize(reader); reader.ReadEndElement();
Type realType = Type.GetType(type);
reader.ReadStartElement("value"); XmlSerializer valueSerializer =new XmlSerializer(realType); object value = valueSerializer.Deserialize(reader); reader.ReadEndElement();
reader.ReadEndElement();
dictionary.Add(key, value); reader.MoveToContent(); } reader.ReadEndElement();
}
publicvoid WriteXml(System.Xml.XmlWriter writer) { XmlSerializer keySerializer =new XmlSerializer(typeof(string)); XmlSerializer typeSerializer =new XmlSerializer(typeof(string)); foreach (string key in dictionary.Keys) { writer.WriteStartElement("item");
writer.WriteStartElement("key"); keySerializer.Serialize(writer, key); writer.WriteEndElement();
writer.WriteStartElement("type"); typeSerializer.Serialize(writer, dictionary[key].GetType().FullName); writer.WriteEndElement();
writer.WriteStartElement("value"); XmlSerializer valueSerializer =new XmlSerializer(dictionary[key].GetType()); valueSerializer.Serialize(writer, dictionary[key]); writer.WriteEndElement();
writer.WriteEndElement(); } }
#endregion }
这样这个对象就可以xml序列化了,但是当它作为一个属性创建时,申明是dynamic的
Xml序列化需要的是确定的类型,会报错
Test method Goline.Test.GoblineCreatureTest.TestSave threw exception: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type Skelectone.SkelectoneDynamicObject may not be used in this context. To use Skelectone.SkelectoneDynamicObject as a parameter, return type, or member of a class or struct, the parameter, return type, or member must be declared as type Skelectone.SkelectoneDynamicObject (it cannot be object). Objects of type Skelectone.SkelectoneDynamicObject may not be used in un-typed collections, such as ArrayLists.
为此,我们构造一个用来序列化的对象

///<summary> /// 可序列化的动态属性 /// 仅供系统内部使用,请不要使用该属性 ///</summary> public SkelectoneDynamicObject SerializableExtendedProperties { get { return ExtendedProperties; } set { ExtendedProperties = value; } }
这样就实现了DynamicObject的Xml序列化,Binary序列化也可以类似实现ISerializable接口,这里就不给出实现了
转自:http://www.cnblogs.com/ueqtxu/archive/2010/07/20/1781559.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2009-09-11 如何在子线程中操作主线程的控件
2008-09-11 异步调用与多线程
2008-09-11 ASP.NET 2.0 中的窗体身份验证
2008-09-11 Form验证实战(转)
2008-09-11 ASP.NET 安全认证(如何运用 Form 表单认证)