<PRE lang = cs id = pre2 style = "margin-top: 0px" >/// <summary>
/// <B> baseobject </B> class is an abstract class for you to derive from.
/// Every class that will be dirived from this class will support
/// <B> clone </B> method automatically.
/// The class implements the interface <I> icloneable </I> and there
/// For every object that will be derived
/// From this object will support the <I> icloneable </I> interface as well.
/// </Summary>
Public abstract class baseobject: icloneable
{
/// <Summary>
/// Clone the object, and returning a reference to a cloned object.
/// </Summary>
/// <Returns> reference to the new cloned
/// Object. </returns>
Public object clone ()
{
// First we create an instance of this specific type.
Object newobject = activator. createinstance (this. GetType ());
// We get the array of fields for the new type instance.
Fieldinfo [] fields = newobject. GetType (). getfields ();
Int I = 0;
foreach (fieldinfo fi in this. getType (). getfields ()
{< br> // We query if the fiels support the icloneable interface.
type iclonetype = Fi. fieldtype.
getinterface ("icloneable", true);
If (iclonetype! = NULL)
{
// Getting the icloneable interface from the object.
Icloneable iclone = (icloneable) Fi. getvalue (this );
// We use the clone method to set the new value to the field.
Fields [I]. setvalue (newobject, iclone. Clone ());
}
Else
{
// If the field doesn' t support the icloneable
// Interface then just set it.
Fields [I]. setvalue (newobject, Fi. getvalue (this ));
}
// Now we check if the object supports
// Ienumerable interface, so if it does
// We need to enumerate all its items and check if
// They support the icloneable interface.
Type ienumerabletype = Fi. fieldtype. getinterface
("Ienumerable", true );
If (ienumerabletype! = NULL)
{
// Get the ienumerable interface from the field.
Ienumerable ienum = (ienumerable) Fi. getvalue (this );
// This version supports the ilist and
// Idictionary interfaces to iterate on collections.
Type ilisttype = Fields [I]. fieldtype. getinterface
("Ilist", true );
Type idictype = Fields [I]. fieldtype. getinterface
("Idictionary", true );
Int J = 0;
If (ilisttype! = NULL)
{
// Getting the ilist interface.
Ilist list = (ilist) fields [I]. getvalue (newobject );
Foreach (Object OBJ in ienum)
{
// Checking to see if the current item
// Support the icloneable interface.
Iclonetype = obj. GetType ().
Getinterface ("icloneable", true );
If (iclonetype! = NULL)
{
// If it does support the icloneable interface,
// We use it to set the clone
// The object in the list.
Icloneable clone = (icloneable) OBJ;
List [J] = clone. Clone ();
}
// note: if the item in the list is not
// support the icloneable interface then in the
// cloned list this item will be the same
// item in the original list
// (as long as this type is a reference type ).
J ++;
}
}
Else if (idictype! = NULL)
{
// Getting the dictionary interface.
Idictionary DIC = (idictionary) fields [I].
Getvalue (newobject );
J = 0;
Foreach (dictionaryentry de in ienum)
{
// Checking to see if the item
// Support 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;
}
}
</PRE>