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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:32:39+00:00 2026-05-16T18:32:39+00:00

I am looking to find a way to take two objects that have identical

  • 0

I am looking to find a way to take two objects that have identical properties and make a call to copy the property values from one object into the other. The the example below assume I have an instance of A and I want to use the data of that instance to hydrate a new instance or C (to keep things terse I used fields instead of properties in the example below)

public class A : B
{
    public string prop1;
    public int prop2;
}

public class B
{
    public byte propX;
    public float propY;
}

public class C
{
    public byte propX;
    public float propY;
    public string prop1;
    public int prop2;
}

public class Merger
{
    public static object Merge(object copyFrom, object copyTo)
    { 
        //do some work
        //maybe <T> generically refactor?
    }
}

The merger class is just a psuedo-example, doing this through generics would be optimal but the first thing I question is whether such a capability already exists. I could imagine using reflection to do this myself but just wanted to toss it out for better ideas first.

Real world context: This is actually an MVVM related issue as I am trying to use disparate classes coming back from EF to populate a ViewModel instance.

  • 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-16T18:32:40+00:00Added an answer on May 16, 2026 at 6:32 pm

    Check out tools and libraries like AutoMapper – those would handle cases like this with ease – and much more! No need to re-invent the wheel – just use the tool! 🙂

    You would basically define a map between classes A and C like this:

    Mapper.CreateMap<A, C>();
    

    and then later on, you can have AutoMapper do the mapping, based on that map, from an instance of A into an instance of C, something like this:

    C yourC = Mapper.Map<A, C>(instanceOfA);
    

    AutoMapper does a default mapping based on property names (and types), but you can extend and influence it in a great many ways to include mappings from one property to another, even if the names (or types) don’t match 100%. It’s quite flexible and well established – definitely worth a serious look!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to find a way to access the .net query string contained in
I'm looking for a way to find the name of the Windows default printer
I'm looking for a way to find references to a web method in other
I'm looking for a way to find out the URL of a SWF using
I'm looking for a way to find the status of a live stream through
As a way to find inspiration and improve my PHP skills, I am looking
I'm looking to find records in a table that match a specific number that
I am looking to find a librray that emulates part of the capabilities of
Following are the two approaches: constructor with all the class properties Pros: I have
I have a situation where I'm starting a number of objects that, when they

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.