Deep copy of serialization mode

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.