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

The Archive Base Latest Questions

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

I want to map all properties that match between two class instances. public class

  • 0

I want to map all properties that match between two class instances.

public class Foo
{
    public bool A { get: set: }
    public bool B { get: set: }
    public bool C { get: set: }
    public bool D { get: set: }
    public bool E { get: set: }
}

public class Bar
{
    public bool A { get: set: }
    public bool B { get: set: }
}

Bar bar = new Bar();
bar.A = true;
bar.B = true;

How do I map the values from the bar instance to a new instance of foo (setting the properties “A” and “B” to true)? I tried to make a method for it, but i get the exception Property set method not found..

public static void MapObjectPropertyValues(object e1, object e2)
    {
        foreach (var p in e1.GetType().GetProperties())
        {
            if (e2.GetType().GetProperty(p.Name) != null)
            {
                p.SetValue(e1, e2.GetType().GetProperty(p.Name).GetValue(e2, null), null);
            }   

        }
    }

Any help is much appreciated, thanks!

  • 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-28T04:54:34+00:00Added an answer on May 28, 2026 at 4:54 am

    I’ve implemented something similar to this before by abstracting the common properties into interfaces and then having a utility class that copied interface properties. What you end up with is something like this:

    public interface IHaveAAndB
    {
        bool A { get; set; }
        bool B { get; set; }
    }
    
    public class Foo : IHaveAAndB
    {     
        public bool A { get; set; }     
        public bool B { get; set; }     
        public bool C { get; set; }     
        public bool D { get; set; }     
        public bool E { get; set; } 
    }  
    
    public class Bar : IHaveAAndB
    {     
        public bool A { get; set; }     
        public bool B { get; set; } 
    } 
    
    // Disclaimer - I've not tested whether this compiles but essentially
    // make the method generic and call it using the interface type
    // and you can then do a copy from one set of properties to the other
    // e.g. CopyInterfaceProperties<IHaveAAndB>(new Foo(), new Bar());
    public static void CopyInterfaceProperties<T>(T e1, T e2)     
    {         
        foreach (var prop in typeof(T).GetProperties())         
        {          
            if (prop.CanRead && prop.CanWrite)  
            {
                var value = prop.GetValue(e2, null)
                prop.SetValue(e1, value, null);   
            }
        }     
    } 
    

    Just be sure to check the property can be read and written to before calling SetValue!

    In the case you cannot abstract into interfaces simply checking CanRead on source and CanWrite on the destination (also checking for existance of the property on the destination) should resolve the issue you have above.

    Best regards,

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

Sidebar

Related Questions

Given the classes: public class Person { public string Name { get; set; }
My User table I want to map to aspnet_Users: <class name=User table=`User`> <id name=ID
I have one very general object that I want to map to a destination
I have a database field called abCode , that I want to map to
How do you map a class to other instances of the same class when
Using Fluent NHibernate I need a clue how to map my Invoice class. public
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
I want to create a method that allows me to change arbitrary properties of
I have two C# classes that have many of the same properties (by name
I want to map a INI file as a python object. So if the

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.