序列化方式深拷贝


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;
}

 

/// <summary>
/// 获取相应的Attribute对象 如 var attr=typeof(Person).GetAttribute<DBTableNameAttribute>();
/// </summary>
/// <typeparam name="T">Attribute类</typeparam>
/// <param name="type">实体类</param>
/// <returns>Attribute对象</returns>
static public T GetAttribute<T>(this Type type) where T:class
{

 

 
Attribute classAttribute = Attribute.GetCustomAttribute(type, typeof(T));

 
return classAttribute as T;
}

 


}
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();

 }

}

 

posted @ 2015-11-26 11:51  zslm___  阅读(317)  评论(0编辑  收藏  举报