ArcObject对象序列化和反序列化
作者:互联网
因为AO对象不支持跨线程,所以当开发多线程应用时,需要将AO对象序列化为字符串(本文只介绍序列化为字符串的情形),然后将序列化字符串传递给工作线程,最后在工作线程里将序列化字符串反序列化为AO对象执行相关操作。
AO对象按序列化方式可分为两种:
1、实现IXMLSerialize接口的类(此处为IXMLSerialize而不是IXMLSerializer)
2、实现IPersistStream接口的类
1、实现IXMLSerialize接口的类的序列化和反序列化
/// <summary> /// 实现IXMLSerialize接口的类的序列化 /// </summary> /// <param name="obj">要序列化的对象</param> /// <returns>序列化后的字符串</returns> private string SerialzedXMLSer(object obj) { string serStr = null; try { //此处为IXMLSerializer不是IXMLSerialize(有r) IXMLSerializer xmlSerializer = new XMLSerializerClass(); serStr = xmlSerializer.SaveToString(obj, null, null); } catch { } return serStr; } /// <summary> /// 实现IXMLSerialize接口的类的反序列化 /// </summary> /// <param name="serStr">序列化字符串</param> /// <returns>反序列化得到的对象</returns> private object DeSerialzedXMLSer(string serStr) { object obj = null; try { ///反序列化 IXMLSerializer xmlSerializer = new XMLSerializerClass(); obj = xmlSerializer.LoadFromString(serStr, null, null); } catch { } return obj; }
2、实现IPersistStream接口的类的序列化和反序列化
此种序列化和反序列化方式多用到IXMLPersistedObject接口
/// <summary> /// 序列化实现IPersistStream接口的对象 /// </summary> /// <param name="obj">要序列化的对象</param> /// <returns>序列化后的字符串</returns> private string SerialzedPersist(object obj) { string serStr = null; try { //序列化 IXMLPersistedObject xmlWrapper = new XMLPersistedObjectClass(); xmlWrapper.Object = obj; IXMLSerializer xmlSerializer = new XMLSerializerClass(); serStr = xmlSerializer.SaveToString(xmlWrapper, null, null); } catch { } return serStr; } /// <summary> /// 反序列化实现IPersistStream接口的对象 /// </summary> /// <param name="serStr">序列化字符串</param> /// <returns>反序列化得到的对象</returns> private object DeSerialzedPersist(string serStr) { object obj = null; try { ///反序列化 IXMLSerializer xmlSerializer = new XMLSerializerClass(); IXMLPersistedObject deWrapper = xmlSerializer.LoadFromString(serStr, null, null) as IXMLPersistedObject; obj = deWrapper.Object; } catch { } return obj; }
标签:序列化,obj,对象,接口,serStr,ArcObject,xmlSerializer,null 来源: https://www.cnblogs.com/songqingguo/p/12819468.html