How do I copy NONSERIALIZED fields and properties of an object?
- From: expertware@xxxxxxxxx
- Date: 9 Sep 2005 06:13:11 -0700
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((BindingFlags.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/
.
- Prev by Date: Re: large object memory leak?
- Next by Date: Re: .NET WindowForm Application Performance Issue
- Previous by thread: Zero byte receive and .net sockets
- Next by thread: RE: Strnage problem with program jumping too 25% cpu
- Index(es):
Relevant Pages
|