Exemplo n.º 1
0
        /// <summary>
        /// Gets a deep copy of a specified object and copies referenced files
        /// Deep copying copies all primitive and enumeration properties and the properties for which MetaInfo "ObjectCopy" is set to true
        /// </summary>
        /// <param name="source">The object to be copied</param>
        /// <param name="path">The path in which copied files will be positioned (relative to original position)</param>
        /// <returns>The copied object</returns>
        public static object GetCopy(object source, string path)
        {
            if (source == null)
            {
                return(null);
            }

            if (source is FileSystemInfo)
            {
                if ((path != null) && (!path.Trim().Equals("")))
                {
                    return(CopyFile((FileSystemInfo)source, path));
                }
                else
                {
                    return(ObjectSupport.GetInstance(source.GetType(), source.ToString()));
                }
            }

            object copy = ObjectSupport.GetInstance(source.GetType(), source.ToString());

            if (copy != null)
            {
                ObjectSupport.Copy(source, copy, path);
            }

            return(copy);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a deep copy of a specified object.
        /// Deep copying copies all primitive and enumeration properties and the properties for which MetaInfo "ObjectCopy" is set to true
        /// </summary>
        /// <param name="source">The object to be copied</param>
        /// <returns>The copied object</returns>
        public static object GetCopy(object source)
        {
            object copy = ObjectSupport.GetInstance(source.GetType());

            if (copy != null)
            {
                ObjectSupport.Copy(source, copy);
            }

            return(copy);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates an object which is a deep copy fo a source object, including copying of referenced file
 /// Deep copying copies all primitive and enumeration properties and the properties for which MetaInfo "ObjectCopy" is set to true
 /// </summary>
 /// <param name="source">The object to be copied</param>
 /// <param name="copiedObjects">Lookup table for already copied objects</param>
 /// <param name="path">Path where files are copied to</param>
 /// <returns></returns>
 private static object GetCopy(object source, Hashtable copiedObjects, string path)
 {
     if (copiedObjects[source] != null)
     {
         return(copiedObjects[source]);
     }
     else if (source is FileSystemInfo)
     {
         object copiedFile = CopyFile((FileSystemInfo)source, path);
         copiedObjects.Add(source, copiedFile);
         return(copiedFile);
     }
     else
     {
         object copy = ObjectSupport.GetInstance(source.GetType());
         if (copy != null)
         {
             copiedObjects.Add(source, copy);
             ObjectSupport.Copy(source, copy, copiedObjects, path);
         }
         return(copy);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new object using a base value (e.g. a string with its value).
 /// Normally primitives, enumerations and some value types can be instantiated this way.
 /// Also types with constructors having one argument can be instantiated.
 /// </summary>
 /// <param name="type">Class type of the object to be instantiated</param>
 /// <param name="baseValue">Value which is passed as argument to the constructor</param>
 /// <returns>New object, null if not possible</returns>
 public static object GetInstance(Type type, object baseValue)
 {
     return(ObjectSupport.GetInstance(type, baseValue, null));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new object using a base value (e.g. a string with its value).
 /// Normally primitives, enumerations and some value types can be instantiated this way.
 /// Also types with constructors having one argument can be instantiated.
 /// </summary>
 /// <param name="classType">Full class name of the object to be instantiated</param>
 /// <param name="baseValue">Value which is passed as argument to the constructor</param>
 /// <returns>New object, null if not possible</returns>
 public static object GetInstance(string classType, object baseValue)
 {
     return(ObjectSupport.GetInstance(ObjectSupport.GetType(classType), baseValue, null));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a new object.
 /// Types with an argumentless constructor can be created this way
 /// </summary>
 /// <param name="classType">Full class name of the object to be instantiated</param>
 /// <returns>New object, null if not possible</returns>
 public static object GetInstance(string classType)
 {
     return(ObjectSupport.GetInstance(ObjectSupport.GetType(classType)));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the deep copy
        /// </summary>
        /// <param name="source">The object to be copied</param>
        /// <param name="target">The object which will be the copy</param>
        /// <param name="copiedObjects">Collection of objects and their copied equivalents. Will be populated and queried during copy.</param>
        /// <param name="path">Relative path to location where files will be copied to</param>
        /// <param name="copyValue">Indicates whether the new object should refer to a copy or to the same object</param>
        private static void Copy(object source, object target, Hashtable copiedObjects, string path, bool copyValue)
        {
            if (source is IList)
            {
                IList copiedArray = (IList)target;
                copiedArray.Clear();
                for (int j = 0; j < ((IList)source).Count; j++)
                {
                    object copiedArrayValue = ((IList)source)[j];
                    if (copyValue)
                    {
                        copiedArrayValue = ObjectSupport.GetCopy(copiedArrayValue, copiedObjects, path);
                    }
                    copiedArray.Add(copiedArrayValue);
                }
            }
            else if (source is IDictionary)
            {
                IDictionary copiedArray = (IDictionary)target;
                copiedArray.Clear();

                IDictionaryEnumerator dictionaryEnumerator = ((IDictionary)source).GetEnumerator();
                while (dictionaryEnumerator.MoveNext())
                {
                    object copiedArrayKey   = dictionaryEnumerator.Key;
                    object copiedArrayValue = dictionaryEnumerator.Value;
                    if (copyValue)
                    {
                        copiedArrayKey   = ObjectSupport.GetCopy(copiedArrayKey, copiedObjects, path);
                        copiedArrayValue = ObjectSupport.GetCopy(copiedArrayValue, copiedObjects, path);
                    }

                    if (!copiedArray.Contains(copiedArrayKey))
                    {
                        copiedArray.Add(copiedArrayKey, copiedArrayValue);
                    }
                }
            }
            // Special handling for files
            else if (source is FileSystemInfo)
            {
                if (copyValue && (path != null) && (!path.Trim().Equals("")))
                {
                    object copiedValue = CopyFile((FileSystemInfo)source, path);
                }
            }
            else
            {
                PropertyInfo[] property = source.GetType().GetProperties();
                for (int i = 0; i < property.Length; i++)
                {
                    object sourceValue = property[i].GetValue(source, null);

                    if (sourceValue != null)
                    {
                        // Default copied value. This is just a reference to the original value
                        object copiedValue = sourceValue;

                        // If the value has been copied before, use the same copied equivalent
                        if (copiedObjects.ContainsKey(sourceValue))
                        {
                            copiedValue = copiedObjects[sourceValue];
                        }
                        else
                        {
                            // Determine whether the property is to be copied
                            bool copyPropertyValue = (Boolean)MetaInfo.GetAttributeDefault(source.GetType(), property[i].Name, "ObjectCopy", false);

                            if (copyPropertyValue)
                            {
                                if (property[i].CanWrite)
                                {
                                    copiedValue = ObjectSupport.GetCopy(sourceValue, copiedObjects, path);
                                }
                                else
                                {
                                    object targetValue = property[i].GetValue(target, null);
                                    Copy(sourceValue, targetValue, copiedObjects, path);
                                }
                            }
                            else if ((sourceValue is IList) || (sourceValue is IDictionary))
                            {
                                object targetValue = property[i].GetValue(target, null);
                                Copy(sourceValue, targetValue, copiedObjects, path, copyPropertyValue);
                            }
                        }

                        // Populate the target object
                        if (property[i].CanWrite)
                        {
                            property[i].SetValue(target, copiedValue, null);
                        }

                        if (!copiedObjects.Contains(sourceValue))
                        {
                            copiedObjects.Add(sourceValue, copiedValue);
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Deep copies all properties of source into the properties of target, including files
 /// Deep copying copies all primitive and enumeration properties and the properties for which MetaInfo "ObjectCopy" is set to true
 /// </summary>
 /// <param name="source">The source object</param>
 /// <param name="target">The target object</param>
 /// <param name="path">Path where files are copied to</param>
 public static void Copy(object source, object target, string path)
 {
     ObjectSupport.Copy(source, target, new Hashtable(), path);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Deep copies all properties of source into the properties of target
 /// Deep copying copies all primitive and enumeration properties and the properties for which MetaInfo "ObjectCopy" is set to true
 /// </summary>
 /// <param name="source">The source object</param>
 /// <param name="target">The target object</param>
 public static void Copy(object source, object target)
 {
     ObjectSupport.Copy(source, target, "");
 }