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 7160399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:23:33+00:00 2026-05-28T13:23:33+00:00

Suppose you have 2 classes like so: public class ClassA { public int X

  • 0

Suppose you have 2 classes like so:

public class ClassA {
    public int X { get; set; }
    public int Y { get; set; }
    public int Other { get; set; }
}

public class ClassB {
    public int X { get; set; }
    public int Y { get; set; }
    public int Nope { get; set; }
}

Now imagine you have an instance of each class and you want to copy the values from a into b. Is there something like MemberwiseClone that would copy the values where the property names match (and of course is fault tolerant — one has a get, and the other a set, etc.)?

var a = new ClassA(); var b = new classB();
a.CopyTo(b); // ??

Something like this is pretty easy in a language like JavaScript.

I’m guessing the answer is no, but maybe there is a simple alternative too. I have written a reflection library to do this, but if built in to C#/.NET at a lower level would probably be more efficient (and why re-invent the wheel).

  • 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-28T13:23:34+00:00Added an answer on May 28, 2026 at 1:23 pm

    There’s nothing in the framework for object-object mapping but there’s a very popular library out there that does this: AutoMapper.

    AutoMapper is a simple little library built to solve a deceptively
    complex problem – getting rid of code that mapped one object to
    another. This type of code is rather dreary and boring to write, so
    why not invent a tool to do it for us?

    By the way, just for learning, here’s a simple way you can implement what you want. I haven’t tested it thoroughly, and it’s nowhere as robust / flexible / performant as AutoMapper, but hopefully there’s something to get out of the general idea:

    public void CopyTo(this object source, object target)
    {
        // Argument-checking here...
    
        // Collect compatible properties and source values
        var tuples = from sourceProperty in source.GetType().GetProperties()
                     join targetProperty in target.GetType().GetProperties() 
                                         on sourceProperty.Name 
                                         equals targetProperty.Name
    
                     // Exclude indexers
                     where !sourceProperty.GetIndexParameters().Any()
                        && !targetProperty.GetIndexParameters().Any()
    
                     // Must be able to read from source and write to target.
                     where sourceProperty.CanRead && targetProperty.CanWrite
    
                     // Property types must be compatible.
                     where targetProperty.PropertyType
                                         .IsAssignableFrom(sourceProperty.PropertyType)
    
                     select new
                     {
                         Value = sourceProperty.GetValue(source, null),
                         Property = targetProperty
                     };
    
        // Copy values over to target.
        foreach (var valuePropertyTuple in tuples)
        {
            valuePropertyTuple.Property
                              .SetValue(target, valuePropertyTuple.Value, null);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes: public class Student { public int Id {get; set;}
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose you have a collection of Foo classes: class Foo { public string Bar;
Suppose I have a class A which depends on 3 other classes X, Y
I am a NHibernate newbie. Suppose you have a Customer class like this public
Suppose I have the following classes: public class Test { public static void main(String[]
suppose you have two (or more) classes with private member vectors: class A {
I have a base class Element and derived two other classes from it, Solid
I have a little C# problem. I have two classes ClassA and ClassB defined

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.