码迷,mamicode.com
首页 > 其他好文 > 详细

序列化方式深拷贝

时间:2015-11-26 12:57:29      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:


namespace CommonClass
{
/// <summary>
/// 对Attribute类扩展方法
/// </summary>
public static class CustomAttribute
{
/// <summary>
/// 判断是否存在相应的特性
/// </summary>
/// <typeparam name="T">特性类</typeparam>
/// <param name="type"></param>
/// <returns></returns>
static public bool HasAttribute<T>(this Type type ) where T:class
{
object[] attributes = type.GetCustomAttributes(false);

foreach (Attribute attr in attributes)
{

//判断Attribute 中是否 为 UniqueColumnAttribute

if (attr is T)
{
return true;

}

}
return false;
}
}
public partial class CustomClone
{

/// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">类T 必须有 Serializable 特性</typeparam>
/// <param name="obj">被实例化的对象</param>
/// <returns>二进制数据</returns>
public static byte[] SerializeObj<T>(T obj) where T : class
{
Type type = typeof(T);
if (type.HasAttribute<SerializableAttribute>())
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Flush();
return stream.ToArray();
}

}
return null ;
}
/// <summary>
/// 反序列化对象
/// </summary>
/// <typeparam name="T">类T 必须有 Serializable 特性</typeparam>
/// <param name="bytes">二进制数据流</param>
/// <returns>返回新实例</returns>
public static T DeserializeObj<T>(byte[] bytes) where T : class
{
Type type = typeof(T);
//T obj = type.Assembly.CreateInstance(type.FullName);
try
{
System.IO.Stream stream = new System.IO.MemoryStream(bytes);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

return (T)formatter.Deserialize(stream);
}
catch(Exception ex)
{
}
return null;
}
/// <summary>
/// 深拷贝
/// </summary>
/// <typeparam name="T">类T 必须有 Serializable 特性</typeparam>
/// <param name="obj">深拷贝对象</param>
/// <returns>新对象</returns>
public static T DeepCopy<T>(T obj) where T : class
{
byte[] buf= SerializeObj(obj);

if(buf==null)

return null;
T newObj = DeserializeObj<T>(buf);
return newObj;
}

}//end class

}//end namespace

 

调用方法

[Serializable ]
public class PersonName
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
[Serializable]
public class Person
{
public PersonName Names { get; set; }
public int Age { get; set; }
}

class Program
{

static void Main(string[] args)
{
Person person = new Person() { Names = new PersonName() { FirstName = "Zhang", LastName = "Aven" }, Age = 32 };
Person newPerson = CommonClass.CustomClone.DeepCopy<Person>(person);

if(newPerson ==null)

Console.WriteLine("深拷贝失败!");

else

{

Console.WriteLine("深拷贝成功!");
newPerson .Names = new PersonName() { FirstName = "Yang", LastName = "Grace" };

}

 Console.Read();

 }

}

 

序列化方式深拷贝

标签:

原文地址:http://www.cnblogs.com/zhshlimi/p/4997257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!