By Manoj G
Posted 1 Aug 2003
Applied TO:
VB.NET, XP, W2K, .NET 1.1, Win9x
Abstract: This article discusses why it is necessary to clone, how to clone and some thinking about cloning. Download Source Files - 10 KB
Introduction
This article I will discuss. NET programming - object clone. The need to discuss the cloning of object clones, methods of cloning of objects, and some thinking about subject clones will be discussed.
background
Everyone must know that the .NET object is two major types of: value types and reference types. Value type object variable indicates the object itself, and has "Copy-on-Assign" behavior. That is, the following discussion is not suitable for value type.
On the other hand, the reference type variable is actually pointing to the reap. Therefore, if you create a variable of a reference type and assign an existing object to it, it is actually another object that is the same memory to the stack. This article is to discuss this situation: a new copy of an object created, and save it in a variable!
Why do you want to clone?
I think it is necessary to obtain an expensive price when setting an object, and a copy of the object is needed to change the current state, clone is very necessary. The following examples are just examples that can reflect what I have just mentioned. Take the DataTable class. Creating a DataTable will include actions such as the following: Query the database, add constraints, set the primary key, and so on for acquiring architecture and data. Then, when a new copy of the DataTable is required, even if it is a very small change or add a new line of records to the architecture, the wise selection will be the object that the existing object is another operation, not to create one. New DataTable, which will require more time and resources.
Clones are also widely used in arrays and aggregates, which often need a copy of the object that has existing objects.
Cloning type
We divide clone into two categories: "deep" clone clone. "Shallow Full" clone get a new example, a copy of the original object type, contains a value type field. However, if the field is a reference type, the reference will be copied instead of a copy reference object. Therefore, the reference to the original object and the reference to the cloned object point to the same object. On the other hand, the "deep" clone of the object contains all copies of the object directly or indirectly referenced by the original object. The following is an example.
Object X Reference Object A, Object A Reference Object M. The object X of the "superficial" clone object Y is also reference object A. In contrast, the object X of the "deep" clone object y, but directly references the object B, and indirectly reference the object n, here, the object B is the copy of the object A, the object N is the copy of the object M.
Realize clone
System.Object provides protected method MEMBERWISECLONE that can be used to achieve "superficial" clone. Since this method is marked as a "protected" level, we can only access the method in inherited or such inside.
.NET defines an Iclonable interface, a class that requires "deep" clone instead of "superficial" clone must implement the interface. We need to provide a good implementation method to achieve the functionality of the Clone method of the interface.
There are many ways to achieve "deep" clones. One method is to serialize the object into the memory stream and then configure to a new object. We will use a binary Formatter class or the SOAP Formatter class to deeply serialize. Do a written formatter published in the business. The problem with this method is that the class and its members (complete class tables) must be marked as serializable, otherwise formatter will errors.
Reflection is another method that can achieve the same purpose. A good article written by Amir Harel attracts me, and he uses this method to provide a good clone implementation. This article is very good! The following is the link: http://www.codeproject.com/csharp/cloneImpl_class.asp
Any method discussed above, requires the membership type of the object to support self-clones to ensure that "deep" clones can be successful. That is, the object must be serializable, or each individual member must provide Iclonable implementation. If not, we can't "deep" clone on the object at all!
Review
Cloning is a good way to provide programmers. However, we should know when to provide such features, and in some cases, it is strictly that objects should not provide this feature. Clone is not supported as an example as the SQLTransaction class. This class represents a transaction in the SQL Server database. Cloning this object does not make sense, because we may not understand a clone of a business of a database! So, if you think the status of the cloned object generates a logical contradiction, it is not necessary to support clone.
(Finish)
See Origi: An Insight Into Cloning Objects in .NET