コード例 #1
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
 /// <summary>
 /// Copies values from the <see cref="T:System.Collections.IDictionary" /> into the properties of the target.
 /// </summary>
 /// <param name="source">The <see cref="T:System.Collections.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 (KeyValuePair <string, object> pair in source)
     {
         if (!settings.IgnoreList.Contains(pair.Key))
         {
             try
             {
                 SetPropertyValue(target, pair.Key, pair.Value, settings.UseDynamicCache);
             }
             catch (Exception exception)
             {
                 if (!settings.SuppressExceptions)
                 {
                     throw new ArgumentException(string.Format("Property '{0}' copy failed.", pair.Key), exception);
                 }
             }
         }
     }
 }
コード例 #2
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Copies values from the source into the target <see cref="T:System.Collections.IDictionary" />.
        /// </summary>
        /// <param name="source">The source object.</param>
        /// <param name="target">The target <see cref="T:System.Collections.IDictionary" />.</param>
        /// <param name="ignoreList">A list of property names to ignore.
        /// These properties will not be added to the targeted <see cref="T:System.Collections.IDictionary" />.</param>
        public static void Copy(object source, IDictionary <string, object> target, params string[] ignoreList)
        {
            ObjectCopierSettings settings = new ObjectCopierSettings {
                IgnoreList = new List <string>(ignoreList)
            };

            Copy(source, target, settings);
        }
コード例 #3
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Copies values from the <see cref="T:System.Collections.Specialized.NameValueCollection" /> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="T:System.Collections.Specialized.NameValueCollection" /> source.</param>
        /// <param name="target">The target object.</param>
        /// <param name="ignoreList">A list of property names to ignore.
        /// These properties will not be set on the target object.</param>
        public static void Copy(NameValueCollection source, object target, params string[] ignoreList)
        {
            ObjectCopierSettings settings = new ObjectCopierSettings {
                IgnoreList = new List <string>(ignoreList)
            };

            Copy(source, target, settings);
        }
コード例 #4
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Copies values from the <see cref="T:System.Collections.IDictionary" /> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="T:System.Collections.IDictionary" /> source.</param>
        /// <param name="target">The target object.</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>
        public static void Copy(IDictionary <string, object> source, object target, bool suppressExceptions, params string[] ignoreList)
        {
            ObjectCopierSettings settings = new ObjectCopierSettings {
                SuppressExceptions = suppressExceptions,
                IgnoreList         = new List <string>(ignoreList)
            };

            Copy(source, target, settings);
        }
コード例 #5
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Copies values from the <see cref="T:System.Collections.Specialized.NameValueCollection" /> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="T:System.Collections.Specialized.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)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            for (int i = 0; i < source.Count; i++)
            {
                if (!string.IsNullOrEmpty(source.Keys[i]))
                {
                    dictionary.Add(source.Keys[i], source[i]);
                }
            }
            Copy((IDictionary <string, object>)dictionary, target, settings);
        }
コード例 #6
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
 /// <summary>
 /// Copies values from the source into the target <see cref="T:System.Collections.IDictionary" />.
 /// </summary>
 /// <param name="source">The source object.</param>
 /// <param name="target">The target <see cref="T:System.Collections.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)
 {
     string[] cachedPropertyNames;
     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();
     }
     if (settings.UseDynamicCache)
     {
         cachedPropertyNames = MethodCaller.GetCachedPropertyNames(source.GetType());
     }
     else
     {
         cachedPropertyNames = MethodCaller.GetPropertyNames(source.GetType());
     }
     foreach (string str in cachedPropertyNames)
     {
         if (!settings.IgnoreList.Contains(str))
         {
             try
             {
                 object obj2 = GetPropertyValue(source, str, settings.UseDynamicCache);
                 target.Add(str, obj2);
             }
             catch (Exception exception)
             {
                 if (!settings.SuppressExceptions)
                 {
                     throw new ArgumentException(string.Format("Property '{0}' copy failed.", str), exception);
                 }
             }
         }
     }
 }
コード例 #7
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Copies values from the <see cref="T:System.Collections.IDictionary" /> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="T:System.Collections.IDictionary" /> source.</param>
        /// <param name="target">The target object.</param>
        public static void Copy(IDictionary <string, object> source, object target)
        {
            ObjectCopierSettings settings = new ObjectCopierSettings();

            Copy(source, target, settings);
        }
コード例 #8
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <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)
        {
            ObjectCopierSettings settings = new ObjectCopierSettings();

            Copy(source, target, settings);
        }
コード例 #9
0
ファイル: ObjectCopier.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Copies values from the <see cref="T:System.Collections.Specialized.NameValueCollection" /> into the properties of the target.
        /// </summary>
        /// <param name="source">The <see cref="T:System.Collections.Specialized.NameValueCollection" /> source.</param>
        /// <param name="target">The target object.</param>
        public static void Copy(NameValueCollection source, object target)
        {
            ObjectCopierSettings settings = new ObjectCopierSettings();

            Copy(source, target, settings);
        }