在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
普通版: public static object CloneObject( object obj ) { using ( MemoryStream memStream = new MemoryStream( ) ) { BinaryFormatter binaryFormatter = new BinaryFormatter( null , new StreamingContext( StreamingContextStates.Clone ) ); binaryFormatter.Serialize( memStream , obj ); memStream.Seek( 0 , SeekOrigin.Begin ); return binaryFormatter.Deserialize( memStream ); } } 泛型版: public static T Clone<T>(T obj) { T ret = default(T); if (obj != null) { XmlSerializer cloner = new XmlSerializer(typeof(T)); MemoryStream stream = new MemoryStream(); cloner.Serialize(stream, obj); stream.Seek(0, SeekOrigin.Begin); ret = (T)cloner.Deserialize(stream); } return ret; } 注意:如果某个类报提示错误:XXX类无法序列化,则要在类名上加上[Serializable] 转载: https://blog.csdn.net/yinbucheng/article/details/62418205
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论