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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:40:12+00:00 2026-05-28T14:40:12+00:00

I’m trying to improve myself with generics but actually I dont know how to

  • 0

I’m trying to improve myself with generics but actually I dont know how to even start this task:

I need to build a method that will record two tables with logs. This method will recieve two objects of the same type and then I need them to be compared.

That the first table will be recorded with the name of the object.

The second table will receive the following data: the first table record ID, field name, old value and new value.

I need this to be build with generics since I have MANY different objects and I’m sure that’s going to be painful and weird to build any kind of switch case for each object that I want to record those information.

I hope you guys help me !

  • 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-28T14:40:13+00:00Added an answer on May 28, 2026 at 2:40 pm

    Unless these objects share a common interface you’ll have to use reflection to get the properties of an object, then loop through them and compare the values between objectA and objectB. Something like this:

    public void CompareTwoObjectsAndSaveChanges<TObjectType>(TObjectType objectA, TObjectType objectB )
        {
            //makes sure both objects match in Type
            if(objectA.GetType() == objectB.GetType())
            {
               //uses reflection to get all the properties of that object.
               foreach (var prop in objectA.GetType().GetProperties())
               {
                   //checks to see if the value is different
                   if(prop.GetValue(objectA, null) != prop.GetValue(objectB, null))
                   {
                       //get the property name and its two different values
                       string nameOfPropertyThatChanges = prop.Name;
                       string objectAValue = prop.GetValue(objectA, null).ToString();
                       string objectBValue = prop.GetValue(objectB, null).ToString();
    
                       //logic to save to database
                   }
               }   
            }
        }
    

    You’ll have to have oldValue and newValue columns be set to varchar or nvarchar in the database and convert all property values to strings before saving, otherwise you can’t do what you are asking. Technically you wouldn’t even have to use generics here as both parameters could simply take an object and the reflection would still work AS LONG AS both objects were the same type. The generic parameter here just makes it a bit more clean and makes sure that the person calling this method can’t pass in two objects of different types accidently.

    EDIT: To get the name of the object for your first table you would do:

    typeof(TObjectType).ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.