Example #1
0
        internal static void Copy(TSource source, TTarget target, PropertyCopyOptions options)
        {
            if (initializationException != null)
            {
                throw initializationException;
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            var checkNulls = options != null && options.IgnoreNulls;
            var checkDefaults = options != null && options.IgnoreDefaults;

            for (int i = 0; i < sourceProperties.Count; i++)
            {
                var property = sourceProperties[i];
                var value = property.GetValue(source, null);

                if (checkDefaults && Equals(value, DefaultValueCache.GetDefault(property.PropertyType)))
                    continue;

                if (checkNulls && ReferenceEquals(value, null))
                    continue;

                targetProperties[i].SetValue(target, value, null);
            }
        }
Example #2
0
 /// <summary>
 /// Copies all public, readable properties from the source object to the
 /// target. The target type does not have to have a parameterless constructor,
 /// as no new instance needs to be created.
 /// </summary>
 /// <remarks>Only the properties of the source and target types themselves
 /// are taken into account, regardless of the actual types of the arguments.</remarks>
 /// <typeparam name="TSource">Type of the source</typeparam>
 /// <typeparam name="TTarget">Type of the target</typeparam>
 /// <param name="source">Source to copy properties from</param>
 /// <param name="target">Target to copy properties to</param>
 public static void Copy<TSource, TTarget>(TSource source, TTarget target, PropertyCopyOptions options = null)
     where TSource : class
     where TTarget : class
 {
     PropertyCopier<TSource, TTarget>.Copy(source, target, options ?? new PropertyCopyOptions());
 }