默认情况下,对继承自DynamicObject的对象进行序列化操作是不会报错的,但是并没有实际序列化出任何东西来

为了让它进行序列化,我们改造一下实现类,实现IXmlSerializable接口

 

继承自DynamicObject的对象的Xml序列化_ico继承自DynamicObject的对象的Xml序列化_序列化_02代码
继承自DynamicObject的对象的Xml序列化_ide_03
  ///<summary>     /// 动态对象     ///</summary>    [Serializable]     publicclass SkelectoneDynamicObject : 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     }
继承自DynamicObject的对象的Xml序列化_ide_03

 

 

这样这个对象就可以xml序列化了,但是当它作为一个属性创建时,申明是dynamic的

 

public dynamic ExtendedProperties =new SkelectoneDynamicObject();

 

 

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.

 

为此,我们构造一个用来序列化的对象

 

继承自DynamicObject的对象的Xml序列化_ico继承自DynamicObject的对象的Xml序列化_序列化_02代码
继承自DynamicObject的对象的Xml序列化_ide_03
        ///<summary>         /// 动态属性         ///</summary>        [XmlIgnore]         public dynamic ExtendedProperties =new SkelectoneDynamicObject();
        ///<summary>         /// 可序列化的动态属性         /// 仅供系统内部使用,请不要使用该属性         ///</summary>        public SkelectoneDynamicObject SerializableExtendedProperties         {             get             {                 return ExtendedProperties;             }             set             {                 ExtendedProperties = value;             }         }
继承自DynamicObject的对象的Xml序列化_ide_03

 

 

这样就实现了DynamicObject的Xml序列化,Binary序列化也可以类似实现ISerializable接口,这里就不给出实现了