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

  • Home
  • SEARCH
  • 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 8316165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:17:56+00:00 2026-06-08T21:17:56+00:00

I have created this Update method public void Update(Person updated) { var oldProperties =

  • 0

I have created this Update method

 public void Update(Person updated)
   {
       var oldProperties = GetType().GetProperties();
       var newProperties = updated.GetType().GetProperties();
       for (var i = 0; i < oldProperties.Length; i++)
       {
           var oldval = oldProperties[i].GetValue(this, null);
           var newval = newProperties[i].GetValue(updated, null);
           if (oldval != newval)
               oldProperties[i].SetValue(this, newval, null);
       }
   }

What it does is comparing two Person objects and if there is any new values. It updates the original object. This works great, but being a lazy programmer, I would like it to be more reusable.

I would like it to work like this.

Person p1 = new Person(){Name = "John"};
Person p2 = new Person(){Name = "Johnny"};

p1.Update(p2);
p1.Name  => "Johnny"

Car c1 = new Car(){Brand = "Peugeot"};
Car c2 = new Car(){Brand = "BMW"};

c1.Update(c2);
c1.Brand => "BMW"

c1.Update(p1); //This should not be possible and result in an error.

I was thinking about using and Abstract Class to hold the Method and then use some Generic, but I don’t know how to make it Class specific.

  • 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-06-08T21:17:57+00:00Added an answer on June 8, 2026 at 9:17 pm
       public static void Update(object original, object updated)
       {
           var oldProperties = original.GetType().GetProperties();
           var newProperties = updated.GetType().GetProperties();
           for (var i = 0; i < oldProperties.Length; i++)
           {
               var oldval = oldProperties[i].GetValue(original, null);
               var newval = newProperties[i].GetValue(updated, null);
               if (!Equals(oldval,newval))
                   oldProperties[i].SetValue(original, newval, null);
           }
       }
    

    or if you want to ensure the same type:

       public static void Update<T>(T original, T updated)
       {
           var properties = typeof(T).GetProperties();
           for (var i = 0; i < properties.Length; i++)
           {
               var oldval = properties[i].GetValue(original, null);
               var newval = properties[i].GetValue(updated, null);
               if (!Equals(oldval,newval))
                   properties[i].SetValue(original, newval, null);
           }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19
I Have XML similar to this(omitted parts for brevity): <uformrecord> <state>Submitted</state> <created>2012-06-19T11:31:54</created> <updated>2012-06-19T11:32:13</updated> <id>53225sas3c1-d727-42cd-93a6-97cd778e5ee9</id>
I have this function to create a request to another file to update the
I have created this simple program to learn shared_ptr using namespace std; #define Yes
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created this Fiddle with this code: This is my problem I want
I have created this function to check abecedarian with while loop (A word is
To explain my question I have created this http://jsfiddle.net/Jams/XNVB2/ here I want when div1
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key
I have created something like this and this . In effect I have a

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.