Soudamini in .net technology 

About Me  Home   SITE MAP     SEARCH


   
The following code shows how to clone any object in a generic way using reflection.
The Clone Method takes the old object and it's type as parameters and then generates a new deep copy of the object.


      public class ObjectCloner

      {

      /// <summary>

      /// Clones the specified old object(must have a default constructor).

      /// It is possible to clone lists and dictionaries too.

      /// </summary>

      /// <param name="oldObject">The old object.</param>

      /// <param name="type">The type.</param>

      /// <returns></returns>

      public object Clone(object oldObject, Type type)

      {

        object newObject = null;

        try

        {

          newObject = Activator.CreateInstance(type);

          FieldInfo[]newFields = newObject.GetType().GetFields

           (BindingFlags.NonPublic |  BindingFlags.Instance);

   

   

          FieldInfo[] oldFields = oldObject.GetType().GetFields

     (BindingFlags.NonPublic | BindingFlags.Instance);

   

          int i = 0;

   

          foreach (FieldInfo oldFieldInfo in oldFields)

          {

            Type ICloneType = oldFieldInfo.FieldType.GetInterface

                              ("ICloneable", true);

   

            Type IEnumerableType = oldFieldInfo.FieldType.GetInterface

                                   ("IEnumerable", true);

   

            if (ICloneType != null)

            {

             ICloneable IClone = (ICloneable)oldFieldInfo.

                                  GetValue(oldObject);

                                  

             if (IClone != null)

                  newFields[i].SetValue(newObject, IClone.Clone());

            }

            else if (IEnumerableType != null)

            {

              IEnumerable IEnum = (IEnumerable)oldFieldInfo.

                                   GetValue(oldObject);

              Type IListType = newFields[i].FieldType.GetInterface

                               ("IList", true);

              Type IDicType = newFields[i].FieldType.GetInterface

                               ("IDictionary", true);

   

              if (IListType != null)

              {

                IList list = (IList)newFields[i].GetValue(newObject);

                if (IEnum != null)

                  foreach (object obj in IEnum)

                  {

                   ICloneType = obj.GetType().GetInterface

                                ("ICloneable", true);

   

                   if (ICloneType != null)

                   {

                     ICloneable clone = (ICloneable)obj;

                     list.Add(clone.Clone());

                   }

                   else

                     list.Add(obj);

                  }

                }

                else if (IDicType != null)

                {

                  IDictionary dic = (IDictionary)newFields[i].

                                     GetValue(newObject);

   

                  foreach (DictionaryEntry de in IEnum)

                  {

                    object key = null;

                    object value = null;

                    ICloneType = de.Key.GetType().GetInterface

                                 ("ICloneable", true);

                    if (ICloneType != null)

                    {

                      ICloneable clone = (ICloneable)de.Key;

                      key = clone.Clone();

                    }

                    else

                    key = de.Key;

                    ICloneType = de.Value.GetType().GetInterface

                                  ("ICloneable", true);

                    if (ICloneType != null)

                    {

                      ICloneable clone = (ICloneable)de.Value;

                      value = clone.Clone();

                    }

                    else

                      value = de.Value;

                   

                    dic.Add(key, value);

                  }

                }

             }

             else

             {

               newFields[i].SetValue(newObject, oldFieldInfo.

                                        GetValue(oldObject));

             }

             i++;

          }

        }

        catch

        {

            throw;

        }

        return newObject;

   

      }

   }



Electronics  rf design  VLSI   Free IEEE papers  VLSI Interview  RFIC Technologies RF design RSS