Copy values from one object to another (different type)
<p>I'd need to copy some properties of one object to another.  However, some of the properties need a type conversion from decimal to int.  </p>

<p>I've found this question very useful:
<a href="http://stackoverflow.com/questions/2624823/copy-values-from-one-object-to-another">Copy values from one object to another</a></p>

<p>However, I can't figure out how to modify the code in Jon Skeet and Marc Gravell's MiscUtil  to check the property types and if the source is decimal and the target is int, to call Convert.ToIn32().</p>

<p>Here is the code from MiscUtil that I'd like to figure out how to modify:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;

namespace MiscUtil.Reflection
{
    /// &lt;summary&gt;
    /// Generic class which copies to its target type from a source
    /// type specified in the Copy method. The types are specified
    /// separately to take advantage of type inference on generic
    /// method arguments.
    /// &lt;/summary&gt;
    public static class PropertyCopy&lt;TTarget&gt; where TTarget : class, new()
    {
        /// &lt;summary&gt;
        /// Copies all readable properties from the source to a new instance
        /// of TTarget.
        /// &lt;/summary&gt;
        public static TTarget CopyFrom&lt;TSource&gt;(TSource source) where TSource : class
        {
            return PropertyCopier&lt;TSource&gt;.Copy(source);
        }

        /// &lt;summary&gt;
        /// Static class to efficiently store the compiled delegate which can
        /// do the copying. We need a bit of work to ensure that exceptions are
        /// appropriately propagated, as the exception is generated at type initialization
        /// time, but we wish it to be thrown as an ArgumentException.
        /// &lt;/summary&gt;
        private static class PropertyCopier&lt;TSource&gt; where TSource : class
        {
            private static readonly Func&lt;TSource, TTarget&gt; copier;
            private static readonly Exception initializationException;

            internal static TTarget Copy(TSource source)
            {
                if (initializationException != null)
                {
                    throw initializationException;
                }
                if (source == null)
                {
                    throw new ArgumentNullException("source");
                }
                return copier(source);
            }

            static PropertyCopier()
            {
                try
                {
                    copier = BuildCopier();
                    initializationException = null;
                }
                catch (Exception e)
                {
                    copier = null;
                    initializationException = e;
                }
            }

            private static Func&lt;TSource, TTarget&gt; BuildCopier()
            {
                ParameterExpression sourceParameter = Expression.Parameter(typeof(TSource), "source");
                var bindings = new List&lt;MemberBinding&gt;();
                foreach (PropertyInfo sourceProperty in typeof(TSource).GetProperties())
                {
                    if (!sourceProperty.CanRead)
                    {
                        continue;
                    }
                    PropertyInfo targetProperty = typeof(TTarget).GetProperty(sourceProperty.Name);
                    if (targetProperty == null)
                    {
                        throw new ArgumentException("Property " + sourceProperty.Name + " is not present and accessible in " + typeof(TTarget).FullName);
                    }
                    if (!targetProperty.CanWrite)
                    {
                        throw new ArgumentException("Property " + sourceProperty.Name + " is not writable in " + typeof(TTarget).FullName);
                    }
                    if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
                    {
                        throw new ArgumentException("Property " + sourceProperty.Name + " has an incompatible type in " + typeof(TTarget).FullName);
                    }
                    bindings.Add(Expression.Bind(targetProperty, Expression.Property(sourceParameter, sourceProperty)));
                }
                Expression initializer = Expression.MemberInit(Expression.New(typeof(TTarget)), bindings);
                return Expression.Lambda&lt;Func&lt;TSource,TTarget&gt;&gt;(initializer, sourceParameter).Compile();
            }
        }
    }
}
</code></pre>
