vb.net serialized Deep Copy

Source: Internet
Author: User
Tags emit

Shallow copy: when a reference member is copied, only the address referenced by the referenced member in the original object is copied to the new object, using the newly copied object,

If you change the reference member within the value may be will be affect the source object

deep copy: When a reference member is copied, it deeply copies all of the referenced members, rather than copying the address reference as simple, the new object contains the

all the members, all the freshmen. Object the new object after using the copy does not affect the source object

Shallow copy on. NET, System::object::memberwiseclone is used directly for quick copy,

    <__dynamicallyinvokable (), securitysafecritical () >    <methodimpl (methodimploptions.internalcall) >    Protected Function MemberwiseClone () as Object

The following is a class for serialized replication, deep Cory deeply copy, shallow copy shallow copy

Classes that need to be serialized must be added at the top of the class <Serializable> attributes otherwise unusable

If members are not allowed to be serialized, you should add <NonSerialized> attributes above the members,

    <Serializable> public    Class deepcopy        Private mvalue As Integer public property        Value As Integer            Get                Return me.mvalue            End Get            set (value as Integer)                Me.mvalue = value            End Set        end Property    End Class
   

The following code is primarily a call to a test that is serialized after replication modifies the values in a member to affect the members of the source object

    Sub Main ()        Dim x = New Deepcopy ()        x.value = $        Dim y as Deepcopy = Clone (x)        y.value =        Console.wri Teline ("x={0}, Y={1}", X.value, Y.value)        Console.readkey (False)    End Sub

Even a shallow copy of the above code does not affect the members of the source object, and I mentioned above that only reference members will be affected

Value type members do not affect each other,

Shallow copy referencing a single reference member pointer still consumes a full 4~8 byte, subject to processor and development platform constraints

    Public Function Clone (ByVal obj) as Object        Using ms as MemoryStream = new MemoryStream ()            Dim fm = new Binaryform Atter ()            FM. Serialize (MS, obj)            Ms. Seek (0, Seekorigin.begin)            Return FM. Deserialize (ms)        End Using    End Function

above is a code for serializing deep replication, creating a memory stream object MemoryStream and create

Binary serialized Object BinaryFormatter

BinaryFormatter:: Serialize//serialization

BinaryFormatter::D eserialize//deserialization

after serializing the object, Stream::seek at the end position, we need to move the position to at the beginning of the stream

You can deserialize an object otherwise it will produce an error

    Public Function il_clone (ByVal obj) as Object Dim dm = New DynamicMethod ("Clone", GetType (Object), {GetType (obj ECT)}) Dim il = DM. GetILGenerator () Dim il_0033 = il. Definelabel () Dim il_de84 = il. DeclareLocal (GetType (MemoryStream)) Dim il_fa10 = il. DeclareLocal (GetType (BinaryFormatter)) Dim il_bcc2 = il. DeclareLocal (GetType (Object)) Dim Il_cs10 = IL. DeclareLocal (GetType (Object)) IL. Emit (Opcodes.newobj, GetType (MemoryStream). GetConstructor ({})) Il. Emit (opcodes.stloc_0) IL. Emit (Opcodes.newobj, GetType (BinaryFormatter). GetConstructor ({})) Il. Emit (opcodes.stloc_1) IL. Emit (opcodes.ldloc_1) IL. Emit (opcodes.ldloc_0) IL. Emit (opcodes.ldarg_0) IL. Emit (Opcodes.callvirt, GetType (BinaryFormatter). GetMethod ("Serialize", {GetType (MemoryStream), GetType (Object)})) Il. Emit (opcodes.ldloc_0) IL. Emit (opcodes.ldc_i4_0) IL. Emit (opcodes.conv_i8) IL.    Emit (OPCODES.LDC_I4_0)    Il. Emit (Opcodes.callvirt, GetType (MemoryStream). GetMethod ("Seek", {GetType (Int64), GetType (SeekOrigin)})) Il. Emit (opcodes.pop) IL. Emit (opcodes.ldloc_1) IL. Emit (opcodes.ldloc_0) IL. Emit (Opcodes.callvirt, GetType (BinaryFormatter). GetMethod ("Deserialize", {GetType (MemoryStream)})) Il. Emit (opcodes.stloc_2) IL. Emit (opcodes.ldloc_0) IL. Emit (Opcodes.callvirt, GetType (MemoryStream). GetMethod ("Close", {})) Il. Emit (opcodes.ldloc_2) IL. Emit (opcodes.stloc_3) IL. Emit (opcodes.br_s, il_0033) IL. Marklabel (il_0033) IL. Emit (opcodes.ldloc_3) IL. Emit (Opcodes.ret) Return DM. Invoke (DM, {obj}) End Function

The above is an embedded IL code that implements a deep copy of an object that is essentially an upper code

The machine notation function is the same as the same procedure is the same as just a call using a close

Diversity is not a very big dependency on namespaces

Imports systemimports system.ioimports System.Reflection.EmitImports System.Runtime.Serialization.Formatters.Binary



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

vb.net serialized Deep Copy

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.