Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. collections;
Using System. reflection;
Namespace Common. cloneobjbase
{
/// <Summary>
/// The baseobject class is an abstract class used for inheritance.
/// Each class inherited from this class automatically supports the clone method.
/// This class implements the icloneable interface, and each object inherited from this object will be the same
/// Supports the icloneable interface.
/// </Summary>
Public Abstract Class Cloneobj: icloneable
{
/// <Summary>
/// Author: Evan Lee
/// QQ: 278631309
/// Blog: Http://www.cnblogs.com/magic_evan
/// Email: can't be found
/// </Summary>
/// <Typeparam name = "T"> The cloned object to be returned. The object must be a class </Typeparam>
/// <Param name = "tobj"> Object instance to be cloned </Param>
/// <Returns> </returns>
Public T clone < T > (T tobj) Where T: Class
{
// First, we create an instance of the specified type.
Object Newobject = Activator. createinstance ( Typeof (T ));
// We get the field array of the New Type instance.
Fieldinfo [] fields = Newobject. GetType (). getfields ();
Int I = 0 ;
Foreach (Fieldinfo fi In Typeof (T). getfields ())
{
// We determine whether the field supports the icloneable interface.
Type iclonetype = Fi. fieldtype. getinterface ( " Icloneable " , True );
If (Iclonetype ! = Null )
{
// Obtain the icloneable interface of the object.
Icloneable iclone = (Icloneable) Fi. getvalue (tobj );
// We use the clone method to set a new value for the field.
Fields [I]. setvalue (newobject, iclone. Clone ());
}
Else
{
// If this field supports the icloneable interface, set it directly.
Fields [I]. setvalue (newobject, Fi. getvalue (tobj ));
}
// Now we check whether this object supports the ienumerable interface. If yes,
// We also need to enumerate all its items and check whether they support the ilist or idictionary interface.
Type ienumerabletype = Fi. fieldtype. getinterface ( " Ienumerable " , True );
If (Ienumerabletype ! = Null )
{
// Ienumerable interface for obtaining this field
Ienumerable ienum = (Ienumerable) Fi. getvalue (tobj );
Type ilisttype = Fields [I]. fieldtype. getinterface ( " Ilist " , True );
Type idictype = Fields [I]. fieldtype. getinterface ( " Idictionary " , True );
Int J = 0 ;
If (Ilisttype ! = Null )
{
// Obtain the ilist interface.
Ilist list = (Ilist) fields [I]. getvalue (newobject );
Foreach ( Object OBJ In Ienum)
{
// Check whether the current item supports the icloneable interface.
Iclonetype = OBJ. GetType (). getinterface ( " Icloneable " , True );
If (Iclonetype ! = Null )
{
// If the icloneable interface is supported,
// We use it to set the object clone in the list.
Icloneable clone = (Icloneable) OBJ;
List [J] = Clone. Clone ();
}
// Note: If the items in the list do not support the icloneable interface
// Items in the clone list will be the same as those in the original list
// (As long as this type is a reference type)
J ++ ;
}
}
Else If (Idictype ! = Null )
{
// Obtain the idictionary Interface
Idictionary DIC = (Idictionary) fields [I]. getvalue (newobject );
J = 0 ;
Foreach (Dictionaryentry de In Ienum)
{
// Check whether the current item supports the icloneable interface.
Iclonetype = De. value. GetType ().
Getinterface ( " Icloneable " , True );
If (Iclonetype ! = Null )
{
Icloneable clone = (Icloneable) De. value;
Dic [de. Key] = Clone. Clone ();
}
J ++ ;
}
}
}
I ++ ;
}
Return Newobject As T;
}
# RegionIcloneable Member
Public Object clone ()
{< br> return base . memberwiseclone ();
// throw new exception (" the method or operation is not implemented. ");
}
# Endregion
}
}