Re: Clone a generic class in a compact framework solution...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Andrew,

The CF has no BinaryFormatter class out of the box, although I believe there are 3rd party products that provide this capability.

--
Ginny Caughey
Device Application Development MVP


"Andrew Brook" <ykoorb@xxxxxxxxxxx> wrote in message news:eVKtfFdBIHA.3716@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

does something like the following not work?

Public Class ObjectCloner
Public Shared Function Clone(obj As Object) As Object
Dim buffer As New MemoryStream()
Dim formatter As New BinaryFormatter()
formatter.Serialize(buffer, obj)
buffer.Position = 0
Return formatter.Deserialize(buffer)
End Function
End Class

Apologies if it errors or doesn't work, I haven't tested it :)

ta,
Andrew

"Mobileboy36" <Mobileboy36@xxxxxxxxx> wrote in message news:4703766c$0$22305$ba620e4c@xxxxxxxxxxxxxxxxx
Hello Group,

How do I take a clone of a generic class in Compact Framework?
Let's say I want to clone a sorted list: SortedList As SortedList(Of Integer, Customer)

I constructed a class like this:
Public Class ClonableAndSortedCustomerList
Inherits SortedList(Of Integer, Customer)

Public Function Clone() As ClonableAndSortedCustomerList
Return DirectCast(Me.MemberwiseClone, ClonableAndSortedCustomerList)
End Function
End Class

Using clone method realy cloned the list, but the items in the list where not cloned.
Changing an item in the orininal list, caused the same change in the 'cloned' list.
So: my conclusion: I need another (correct) way to clone my generic class.

In the full framework it can be realised at this way:
(http://forums.microsoft.com/msdn/showpost.aspx?pageindex=2&siteid=1&postid=2208691&sb=0&d=1&at=7&ft=11&tf=0&pageid=1)
Public Class ObjectCloner
Public Shared Function Clone(Of T)(obj As T) As T

Using buffer As New MemoryStream()

Dim formatter As New BinaryFormatter()

formatter.Serialize(buffer, obj)

buffer.Position = 0

Dim temp As T = DirectCast(formatter.Deserialize(buffer), T)

Return temp

End Using

End Function

End Class

But how to realise it in VB.NET, using a compact framework solution?



Best regards,

Mobile boy







.



Relevant Pages

  • Re: Clone a generic class in a compact framework solution...
    ... I think you'd have to implement your own cloning logic, whether that involved writing your own binary formatter or using Xml, which of course would be bulkier. ... The CF has no BinaryFormatter class out of the box, although I believe there are 3rd party products that provide this capability. ... Let's say I want to clone a sorted list: ... Public Class ClonableAndSortedCustomerList ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Clone a generic class in a compact framework solution...
    ... Public Shared Function CloneAs Object ... Dim formatter As New BinaryFormatter ... Let's say I want to clone a sorted list: ... Public Class ClonableAndSortedCustomerList ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Generic ICloneable
    ... ICloneablethen the object should be an X and you can cast directly to ... public X Clone() {... ... If you had a generic IClonable interface, ... public class MyClass: IClonable ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Clone a generic class in a compact framework solution...
    ... Public Class ObjectCloner ... Public Shared Function CloneAs Object ... Dim formatter As New BinaryFormatter ... Public Class ClonableAndSortedCustomerList ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Baffling compiler warning using clone()
    ... defined a covariant return type for clone(). ... public class TestCovar ... gives the same warning when casting from Object to TestCovaras did ... the cast *outside* of the method with a covariant return type. ...
    (comp.lang.java.programmer)