예제 #1
0
 /// <summary>
 /// Copies values from the source into the properties of the target.
 /// </summary>
 /// <param name="source">An object containing the source values.</param>
 /// <param name="target">An object with properties to be set from the source.</param>
 /// <param name="ignoreList">A list of property names to ignore. 
 /// These properties will not be set on the target object.</param>
 /// <remarks>
 /// The property names and types of the source object must match the property names and types
 /// on the target object. Source properties may not be indexed. 
 /// Target properties may not be readonly or indexed.
 /// </remarks>
 public static void Copy(object source, object target, params string[] ignoreList)
 {
     var settings = new ObjectCopierSettings
     {
         IgnoreList = new List<string>(ignoreList),
     };
     Copy(source, target, settings);
 }
예제 #2
0
        /// <summary>
        /// Copies values from the source into the properties of the target.
        /// </summary>
        /// <param name="source">An object containing the source values.</param>
        /// <param name="target">An object with properties to be set from the source.</param>
        /// <param name="ignoreList">A list of property names to ignore.
        /// These properties will not be set on the target object.</param>
        /// <remarks>
        /// The property names and types of the source object must match the property names and types
        /// on the target object. Source properties may not be indexed.
        /// Target properties may not be readonly or indexed.
        /// </remarks>
        public static void Copy(object source, object target, params string[] ignoreList)
        {
            var settings = new ObjectCopierSettings
            {
                IgnoreList = new List <string>(ignoreList),
            };

            Copy(source, target, settings);
        }
예제 #3
0
        /// <summary>
        /// Copies values from the source into the properties of the target.
        /// </summary>
        /// <param name="source">An object containing the source values.</param>
        /// <param name="target">An object with properties to be set from the source.</param>
        /// <param name="ignoreList">A list of property names to ignore.
        /// These properties will not be set on the target object.</param>
        /// <param name="suppressExceptions">If <see langword="true" />, any exceptions will be suppressed.</param>
        /// <remarks>
        /// <para>
        /// The property names and types of the source object must match the property names and types
        /// on the target object. Source properties may not be indexed.
        /// Target properties may not be readonly or indexed.
        /// </para><para>
        /// Properties to copy are determined based on the source object. Any properties
        /// on the source object marked with the <see cref="BrowsableAttribute"/> equal
        /// to false are ignored.
        /// </para>
        /// </remarks>
        public static void Copy(object source, object target, bool suppressExceptions, params string[] ignoreList)
        {
            var settings = new ObjectCopierSettings
            {
                SuppressExceptions = suppressExceptions,
                IgnoreList         = new List <string>(ignoreList),
            };

            Copy(source, target, settings);
        }
예제 #4
0
        /// <summary>
        /// Copies values from the <see cref="NameValueCollection"/> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="NameValueCollection"/> source.</param>
        /// <param name="target">The target object.</param>
        /// <param name="settings">The settings to use when copying properties.</param>
        public static void Copy(NameValueCollection source, object target, ObjectCopierSettings settings)
        {
            var newSource = new Dictionary <string, object>();

            for (int i = 0; i < source.Count; i++)
            {
                if (!String.IsNullOrEmpty(source.Keys[i]))
                {
                    newSource.Add(source.Keys[i], source[i]);
                }
            }

            Copy(newSource, target, settings);
        }
예제 #5
0
        /// <summary>
        /// Copies values from the source into the properties of the target.
        /// </summary>
        /// <param name="source">An object containing the source values.</param>
        /// <param name="target">An object with properties to be set from the source.</param>
        /// <param name="settings">The settings to use when copying properties.</param>
        /// <remarks>
        ///     <para>
        /// The property names and types of the source object must match the property names and types
        /// on the target object. Source properties may not be indexed.
        /// Target properties may not be readonly or indexed.
        /// </para><para>
        /// Properties to copy are determined based on the source object. Any properties
        /// on the source object marked with the <see cref="BrowsableAttribute"/> equal
        /// to false are ignored.
        /// </para>
        /// </remarks>
        public static void Copy(object source, object target, ObjectCopierSettings settings)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source", "Source object can not be Null.");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target", "Target object can not be Null.");
            }

            if (settings == null)
            {
                settings = new ObjectCopierSettings();
            }

            string[] sourceProperties;
            if (settings.UseDynamicCache)
            {
                sourceProperties = MethodCaller.GetCachedPropertyNames(source.GetType());
            }
            else
            {
                sourceProperties = MethodCaller.GetPropertyNames(source.GetType());
            }

            foreach (string propertyName in sourceProperties)
            {
                if (settings.IgnoreList.Contains(propertyName))
                {
                    continue;
                }

                try
                {
                    object value = GetPropertyValue(source, propertyName, settings.UseDynamicCache);
                    SetPropertyValue(target, propertyName, value, settings.UseDynamicCache);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Property '{0}' copy failed.", propertyName));
                    if (!settings.SuppressExceptions)
                    {
                        throw new InvalidOperationException(
                                  String.Format("Property '{0}' copy failed.", propertyName), ex);
                    }
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Copies values from the <see cref="IDictionary"/> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="IDictionary"/> source.</param>
        /// <param name="target">The target object.</param>
        /// <param name="settings">The settings to use when copying properties.</param>
        public static void Copy(IDictionary <string, object> source, object target, ObjectCopierSettings settings)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source", "Source object can not be Null.");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target", "Target object can not be Null.");
            }
            if (settings == null)
            {
                settings = new ObjectCopierSettings();
            }

            foreach (var item in source)
            {
                if (settings.IgnoreList.Contains(item.Key))
                {
                    continue;
                }

                try
                {
                    SetPropertyValue(target, item.Key, item.Value, settings.UseDynamicCache);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Property '{0}' copy failed.", item.Key));
                    if (!settings.SuppressExceptions)
                    {
                        throw new ArgumentException(
                                  String.Format("Property '{0}' copy failed.", item.Key), ex);
                    }
                }
            }
        }
예제 #7
0
 /// <summary>
 /// Copies values from the source into the properties of the target.
 /// </summary>
 /// <param name="source">An object containing the source values.</param>
 /// <param name="target">An object with properties to be set from the source.</param>
 /// <param name="ignoreList">A list of property names to ignore. 
 /// These properties will not be set on the target object.</param>
 /// <param name="suppressExceptions">If <see langword="true" />, any exceptions will be suppressed.</param>
 /// <remarks>
 /// <para>
 /// The property names and types of the source object must match the property names and types
 /// on the target object. Source properties may not be indexed. 
 /// Target properties may not be readonly or indexed.
 /// </para><para>
 /// Properties to copy are determined based on the source object. Any properties
 /// on the source object marked with the <see cref="BrowsableAttribute"/> equal
 /// to false are ignored.
 /// </para>
 /// </remarks>
 public static void Copy(object source, object target, bool suppressExceptions, params string[] ignoreList)
 {
     var settings = new ObjectCopierSettings
     {
         SuppressExceptions = suppressExceptions,
         IgnoreList = new List<string>(ignoreList),
     };
     Copy(source, target, settings);
 }
예제 #8
0
 /// <summary>
 /// Copies values from the source into the properties of the target.
 /// </summary>
 /// <param name="source">An object containing the source values.</param>
 /// <param name="target">An object with properties to be set from the source.</param>
 /// <remarks>
 /// The property names and types of the source object must match the property names and types
 /// on the target object. Source properties may not be indexed. 
 /// Target properties may not be readonly or indexed.
 /// </remarks>
 public static void Copy(object source, object target)
 {
     var settings = new ObjectCopierSettings();
     Copy(source, target, settings);
 }
예제 #9
0
        /// <summary>
        /// Copies values from the <see cref="IDictionary"/> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="IDictionary"/> source.</param>
        /// <param name="target">The target object.</param>
        /// <param name="settings">The settings to use when copying properties.</param>
        public static void Copy(IDictionary<string, object> source, object target, ObjectCopierSettings settings)
        {
            if (source == null)
                throw new ArgumentNullException("source", "Source object can not be Null.");
            if (target == null)
                throw new ArgumentNullException("target", "Target object can not be Null.");
            if (settings == null)
                settings = new ObjectCopierSettings();

            foreach (var item in source)
            {
                if (settings.IgnoreList.Contains(item.Key))
                    continue;
                
                try
                {
                    SetPropertyValue(target, item.Key, item.Value, settings.UseDynamicCache);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Property '{0}' copy failed.", item.Key));
                    if (!settings.SuppressExceptions)
                        throw new ArgumentException(
                            String.Format("Property '{0}' copy failed.", item.Key), ex);
                }
            }
        }
예제 #10
0
 /// <summary>
 /// Copies values from the <see cref="IDictionary"/> into the properties of the target.
 /// </summary>
 /// <param name="source">The <see cref="IDictionary"/> source.</param>
 /// <param name="target">The target object.</param>
 public static void Copy(IDictionary<string, object> source, object target)
 {
     var settings = new ObjectCopierSettings();
     Copy(source, target, settings);
 }
예제 #11
0
        /// <summary>
        /// Copies values from the <see cref="NameValueCollection"/> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="NameValueCollection"/> source.</param>
        /// <param name="target">The target object.</param>
        /// <param name="settings">The settings to use when copying properties.</param>
        public static void Copy(NameValueCollection source, object target, ObjectCopierSettings settings)
        {
            var newSource = new Dictionary<string, object>();
            for (int i = 0; i < source.Count; i++)
                if (!String.IsNullOrEmpty(source.Keys[i]))
                    newSource.Add(source.Keys[i], source[i]);

            Copy(newSource, target, settings);
        }
예제 #12
0
 /// <summary>
 /// Copies values from the <see cref="NameValueCollection"/> into the properties of the target.
 /// </summary>
 /// <param name="source">The <see cref="NameValueCollection"/> source.</param>
 /// <param name="target">The target object.</param>
 public static void Copy(NameValueCollection source, object target)
 {
     var settings = new ObjectCopierSettings();
     Copy(source, target, settings);
 }
예제 #13
0
        /// <summary>
        /// Copies values from the source into the target <see cref="IDictionary"/>.
        /// </summary>
        /// <param name="source">The source object.</param>
        /// <param name="target">The target <see cref="IDictionary"/>.</param>
        /// <param name="settings">The settings to use when copying properties.</param>
        public static void Copy(object source, IDictionary<string, object> target, ObjectCopierSettings settings)
        {
            if (source == null)
                throw new ArgumentNullException("source", "Source object can not be Null.");
            if (target == null)
                throw new ArgumentNullException("target", "Target object can not be Null.");
            if (settings == null)
                settings = new ObjectCopierSettings();

            string[] sourceProperties;
            if (settings.UseDynamicCache)
                sourceProperties = MethodCaller.GetCachedPropertyNames(source.GetType());
            else
                sourceProperties = MethodCaller.GetPropertyNames(source.GetType());

            foreach (string propertyName in sourceProperties)
            {
                if (settings.IgnoreList.Contains(propertyName))
                    continue;

                try
                {
                    object value = GetPropertyValue(source, propertyName, settings.UseDynamicCache);
                    target.Add(propertyName, value);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Property '{0}' copy failed.", propertyName));
                    if (!settings.SuppressExceptions)
                        throw new ArgumentException(
                            String.Format("Property '{0}' copy failed.", propertyName), ex);
                }
            }
        }
예제 #14
0
        /// <summary>
        /// Copies values from the source into the properties of the target.
        /// </summary>
        /// <param name="source">An object containing the source values.</param>
        /// <param name="target">An object with properties to be set from the source.</param>
        /// <remarks>
        /// The property names and types of the source object must match the property names and types
        /// on the target object. Source properties may not be indexed.
        /// Target properties may not be readonly or indexed.
        /// </remarks>
        public static void Copy(object source, object target)
        {
            var settings = new ObjectCopierSettings();

            Copy(source, target, settings);
        }
예제 #15
0
        /// <summary>
        /// Copies values from the <see cref="IDictionary"/> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="IDictionary"/> source.</param>
        /// <param name="target">The target object.</param>
        public static void Copy(IDictionary <string, object> source, object target)
        {
            var settings = new ObjectCopierSettings();

            Copy(source, target, settings);
        }
예제 #16
0
        /// <summary>
        /// Copies values from the <see cref="NameValueCollection"/> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="NameValueCollection"/> source.</param>
        /// <param name="target">The target object.</param>
        public static void Copy(NameValueCollection source, object target)
        {
            var settings = new ObjectCopierSettings();

            Copy(source, target, settings);
        }