Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 716503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:18:24+00:00 2026-05-14T05:18:24+00:00

Anyone have a suggestion for a good utility class that maps values from one

  • 0

Anyone have a suggestion for a good utility class that maps values from one object to another? I want a utility class that uses reflection and takes two objects and copies values from the 1st object to the second if there is a public property with the same name.

I have two entities that are generated from a web service proxy, so I can’t change the parent class or impliment an interface or anything like that. But I know that the two objects have the same public properties.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:18:24+00:00Added an answer on May 14, 2026 at 5:18 am

    Jon Skeet and Marc Gravell have a library called MiscUtil. Inside MiscUtil.Reflection there is a class called PropertyCopy that does exactly what you describe. It only works for .NET 3.5.

    It works by running over the public properties of the SourceType, matches them up by name with the public properties of the TargetType, makes sure that each property can be assigned from the source to the target and then creates and caches a copier function for those two types (so you don’t do all this reflection every time). I’ve used it in production code and can vouch for its goodness.

    What the hey, I figured I’d just post their concise code (it’s less than 100 lines w/comments). The license for this code can be found here:

    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    using System.Reflection;
    
    namespace MiscUtil.Reflection
    {
        /// <summary>
        /// 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.
        /// </summary>
        public static class PropertyCopy<TTarget> where TTarget : class, new()
        {
            /// <summary>
            /// Copies all readable properties from the source to a new instance
            /// of TTarget.
            /// </summary>
            public static TTarget CopyFrom<TSource>(TSource source) where TSource : class
            {
                return PropertyCopier<TSource>.Copy(source);
            }
    
            /// <summary>
            /// 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.
            /// </summary>
            private static class PropertyCopier<TSource> where TSource : class
            {
                private static readonly Func<TSource, TTarget> 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<TSource, TTarget> BuildCopier()
                {
                    ParameterExpression sourceParameter = Expression.Parameter(typeof(TSource), "source");
                    var bindings = new List<MemberBinding>();
                    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<Func<TSource,TTarget>>(initializer, sourceParameter).Compile();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a excel-like grid control in MFC, do anyone have good suggestion to
Does anyone have a good suggestion on how to do video recording? We have
Does anyone have any good suggestions for creating a Pipe object in Java which
Does anyone have good suggestion of the best option for jQuery Ajax to refresh
Anyone have suggestions on good tutorials or examples of how to create MSI packages
Does anyone have any suggestions for a good approach to finding all the CPAN
Does anyone have a suggestion for where to find archives or collections of everyday
Does anyone have a suggestion for creating paragraph-type line spaces within a <li> tag
I have a college project that I want to do as 3 Tier Winform
Does anyone have a good and efficient extension method for finding if a sequence

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.