C++/CLI: Object Clone

Basically, Clone can be implemented either as a deep copy or a shallow copy. In a deep copy, all objects are duplicated; whereas, in a shallow copy, only the top-level objects are duplicated and the lower levels containreferences.

But here we work under System::Runtime::Serialization::Formatters::Binary

Step 1: Serialize your object
Step 2: And then Deserialize  your object
Step 3: Close you MemoryStream instance
Step 4: Return Object
That’s it..

So here is the method,

public: System::Object^ Clone(System::Object^ apObj )
{
MemoryStream^ ms = gcnew MemoryStream();
BinaryFormatter^ bf = gcnew BinaryFormatter();
bf->Serialize( ms, apObj );
ms->Position = 0;
System::Object^ obj = bf->Deserialize( ms );
ms->Close();
return obj;
}

This is most fast way to clone your object very easily. So fulks ..

enjoy 😉

Advertisements

About Tanveer
RIA (Rich Internet Application) Developer with a wide variety of business applications. Particularly interested in client/server and relational database design. Always interested in migration projects, as well as close interaction with the DB manufacturers.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: