Namespace Commonclass
{
<summary>
Extension methods for attribute classes
</summary>
public static class CustomAttribute
{
<summary>
Determine if there is a corresponding feature
</summary>
<typeparam name= "T" > Feature class </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)
{
Determine if the attribute is Uniquecolumnattribute
if (attr is T)
{
return true;
}
}
return false;
}
}
public partial class Customclone
{
<summary>
Serializing objects
</summary>
<typeparam name= "T" > class T must have Serializable features </typeparam>
<param name= "obj" > Objects instantiated </param>
<returns> binary Data </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>
Deserializing objects
</summary>
<typeparam name= "T" > class T must have Serializable features </typeparam>
<param name= "bytes" > Binary Data Flow </param>
<returns> return to new instance </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>
Deep copy
</summary>
<typeparam name= "T" > class T must have Serializable features </typeparam>
<param name= "obj" > Deep copy Objects </param>
<returns> new Objects </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
Calling methods
[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 ("Deep copy failed!");
Else
{
Console.WriteLine ("Deep copy success!");
Newperson. Names = new PersonName () {FirstName = "Yang", LastName = "Grace"};
}
Console.read ();
}
}
Deep copy of serialization mode