How do I copy NONSERIALIZED fields and properties of an object?



To make deep clone of object I often use a serialization
in memory: see for instance this snippet:

http://www.freevbcode.com/ShowCode.Asp?ID=5552

This is a very useful technique. Can be used for instance
when you cancel a dialog box to restore the object which
the user is editing.
However, when an object is cloned through serialization in
memory, to make a copy. clearly the <NonSerialized> fields are not
copied.
To copy them one could use something like:

'------------------ copying non serialized ---------

Public Sub CopiaFieldsNotSerialized(ByVal Destinazione As Object,
ByVal Origine As Object)


Dim FieldInfos As FieldInfo() =
Origine.GetType.GetFields((Bin­dingFlags.Static Or
BindingFlags.Instance
Or BindingFlags.NonPublic Or BindingFlags.Public))
For Each FieldInfo As FieldInfo In FieldInfos
With FieldInfo
If .IsNotSerialized Then
.SetValue(Destinazione, .GetValue(Origine))
End If
End With
Next FieldInfo


End Sub
'-----------------------------­-------------

while the above works for some most common cases, it does not work in
general. Two are the problems:

- It must be generalized by introducing recursion. In fact
if a NonSerialized member class has other NonSerialed fields
these will not be relinked by the above Sub.

- Another problem is breaking circular reference. In fact
it is common that there is a nonserialized reference to a parental
class.

Can anybody make suggestions on how to to improve the above to solve
these problems?

Thank you very much.
-Pamela
..NET developer
Datatime Time
http://151.100.3.84/technicalpreview/

.



Relevant Pages

  • Re: memory leak using system.windows.forms.timer
    ... > subroutine, memory leaks. ... > Private Sub CheckComputers() ... > Dim intCount As Integer ... > Dim mySearcher As New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: memory leak using system.windows.forms.timer
    ... subroutine, memory leaks. ...     Private Sub CheckComputers() ... subroutine with a static number I still leak memory. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: share a structure array containing multidimensional char array
    ... When you want to deserialize/serialize a managed type to/from unmanaged memory, you have to use the same serializer/deserializer at both sides, that means you also need to use .NET to serialize/deserialize at the C++ side. ... Besides the structures, you also have to store the number of structures serialized to the shared memory buffer and the size of the individual structure members, without this info it's impossible for the reader to determine the number of array members and the size of the structure members. ... HANDLE hKernel; ... > Mapping a datastructure, whatever it's type, in a serialization. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: share a structure array containing multidimensional char array
    ... memory, you have to use the same serializer/deserializer at both sides, that ... Deserialization Error: ... C++ code doesnot make any serialization. ... instance as an int value at the start of the MM segment. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Memory Problem
    ... > I am using a 64MB device -- and have been pinvoking GlobalMemoryStatusCE ... > ** % Load from Memory Load returned from GlobalMemoryStatusCE ... > Each time I see the 5MB array allocation reflected properly. ... >>> Public Sub New ...
    (microsoft.public.dotnet.framework.compactframework)