public bool IsEqual(IClone other) { //1. make sure that the 'other' object is pointing to a valid object if (null == other) { throw new COMException("Invalid object."); } //2. verify the type of 'other' if (!(other is ClonableObjClass)) { throw new COMException("Bad object type."); } ClonableObjClass otherClonable = (ClonableObjClass)other; //test that all ot the object's properties are the same. //please note the usage of IsEqual when using ArcObjects components that //supports cloning if (otherClonable.Version == m_version && otherClonable.Name == m_name && otherClonable.ID == m_ID && otherClonable.ManagedArray == m_arr && ((IClone)otherClonable.SpatialReference).IsEqual((IClone)m_spatialRef) && ((IClone)otherClonable.Point).IsEqual((IClone)m_point)) { return(true); } return(false); }
public void Test() { Console.WriteLine("Creating an instance of the clonable object."); ClonableObjClass cloneable = new ClonableObjClass(); Console.WriteLine("Assigning properties"); cloneable.SpatialReference = CreateGeographicSpatialReference(); cloneable.Name = "test 1"; cloneable.Point = new PointClass(); cloneable.Point.PutCoords(35.02, 31.4); cloneable.ManagedArray = new ArrayList(); cloneable.ManagedArray.Add("One"); cloneable.ManagedArray.Add("Two"); cloneable.ManagedArray.Add("Three"); Console.WriteLine("New object's properties"); Console.WriteLine("-----------------------"); Console.WriteLine("Name: " + cloneable.Name); Console.WriteLine("ID: " + cloneable.ID.ToString()); Console.WriteLine("Version: " + cloneable.Version.ToString()); Console.WriteLine("Array values: "); foreach (object obj in cloneable.ManagedArray) { Console.WriteLine((string)obj); } Console.WriteLine("Spatial Reference parameters:"); Console.WriteLine("Name: " + cloneable.SpatialReference.Name); IGeographicCoordinateSystem gcs = (IGeographicCoordinateSystem)cloneable.SpatialReference; Console.WriteLine("Alias: " + gcs.Alias); Console.WriteLine("Datum: " + gcs.Datum.Name); Console.WriteLine(""); Console.WriteLine("Cloning the object..."); ClonableObjClass clonee = cloneable.Clone() as ClonableObjClass; Console.WriteLine(""); Console.WriteLine("Cloned object's properties:"); Console.WriteLine("---------------------------"); Console.WriteLine("Name: " + clonee.Name); Console.WriteLine("ID: " + clonee.ID.ToString()); Console.WriteLine("Version: " + clonee.Version.ToString()); Console.WriteLine("Array values: "); foreach (object obj in clonee.ManagedArray) { Console.WriteLine((string)obj); } Console.WriteLine("Spatial Reference parameters:"); Console.WriteLine("Name: " + clonee.SpatialReference.Name); gcs = (IGeographicCoordinateSystem)clonee.SpatialReference; Console.WriteLine("Alias: " + gcs.Alias); Console.WriteLine("Datum: " + gcs.Datum.Name); Console.WriteLine(""); }
public void Assign(IClone src) { //1. make sure that src is pointing to a valid object if (null == src) { throw new COMException("Invalid object."); } //2. make sure that the type of src is of type 'ClonableObjClass' if (!(src is ClonableObjClass)) { throw new COMException("Bad object type."); } //3. assign the properties of src to the current instance ClonableObjClass srcClonable = (ClonableObjClass)src; m_name = srcClonable.Name; m_version = srcClonable.Version; //it is not possible to copy guids... //m_ID = srcClonable.ID; //don't clone the spatial reference, since in this case we want both object to //reference the same spatial reference (for example like features in a featureclass //which share the same spatial reference) m_spatialRef = srcClonable.SpatialReference; //clone the point. Use deep cloning if (null == srcClonable.Point) { m_point = null; } else { IObjectCopy objectCopy = new ObjectCopyClass(); object obj = objectCopy.Copy((object)srcClonable.Point); m_point = (IPoint)obj; } m_arr = (ArrayList)srcClonable.ManagedArray.Clone(); }