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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:53:04+00:00 2026-06-04T00:53:04+00:00

I have a couple of complex objects that I’d like to compare properties on.

  • 0

I have a couple of complex objects that I’d like to compare properties on. The following code does a fine job, up until you get to a collection. I’d like to recursively call the function with each member of the collection. Can someone take a look and help me to determine the type of object in the collection so that I can call HasPropertyChanged again?

This pseudocode shows my intent

      if (p.GetType() == typeof(System.Collections.Generic.List<>))
       {
            foreach (var blah in //the list)
            {
                HasPropertyChanged<//TheType>(Type obj1, Type obj2, null);
            }
        }

Also, this part of the code bugs me. If I don’t call the tostring method, I’ll get some funky results, like id 63633 not equaling 63633

    object val1 = Original.GetType().GetProperty(p.Name).GetValue(Original, null);
        object val2 = Modified.GetType().GetProperty(p.Name).GetValue(Modified, null);
        if (!IgnoreProperties.Contains(p.Name) && 
            val1 != null && val2 != null && 
            val1.ToString() != val2.ToString())
        {
            return true;
        }

Here it is in it’s entirety.

private bool HasPropertyChanged<T>(T Original, T Modified, string[] IgnoreProperties)
    {
        if (Original == null || Modified == null)
            return false;
        if (IgnoreProperties == null)
            IgnoreProperties = new string[] { };

        IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();

        foreach (var p in properties)
        {
            if (p.GetType() == typeof(System.Collections.Generic.List<>))
            {
                foreach (var blah in //the list)
                {
                    HasPropertyChanged<//TheType>(Type obj1, Type obj2, null);
                }
            }
            object val1 = Original.GetType().GetProperty(p.Name).GetValue(Original, null);
            object val2 = Modified.GetType().GetProperty(p.Name).GetValue(Modified, null);
            if (!IgnoreProperties.Contains(p.Name) && 
                val1 != null && val2 != null && 
                val1.ToString() != val2.ToString())
            {
                return true;
            }
        }
        return false;
    }
  • 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-04T00:53:05+00:00Added an answer on June 4, 2026 at 12:53 am

    Since you’re dealing with types at runtime, you should have a non-generic version of your function which takes a Type parameter:

    private bool HasPropertyChanged<T>(T Original, T Modified, string[] IgnoreProperties)
    {
        return HasPropertyChanged(typeof(T), Original, Modified, IgnoreProperties);
    }
    
    private bool HasPropertyChanged(Type T, object Original, object Modified, string[] IgnoreProperties)
    {
        // ...
    }
    

    With that, you can call the method for the list elements:

    if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.List<>))
    {
        object val1 = p.GetValue(Original, null);
        object val2 = p.GetValue(Modified, null);
    
        // First check count...
        PropertyInfo countProperty = p.PropertyType.GetProperty("Count");
        int count1 = countProperty.GetValue(val1, null);
        int count2 = countProperty.GetValue(val2, null);
        if (count1 != count2) return true;
    
        // Now iterate:
        var enumerator1 = (val1 as System.Collections.IEnumerable).GetEnumerator();
        var enumerator2 = (val2 as System.Collections.IEnumerable).GetEnumerator();
        while (enumerator1.MoveNext())
        {
            enumerator2.MoveNext();
            // check for null, skipping here...
            object elem1 = enumerator1.Current;
            object elem2 = enumerator2.Current;
            if (HasPropertyChanged(elem1.GetType(), elem1, elem2, IgnoreProperties)) return true;
        }
    }
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of places that have some code like this: <cfinvoke component=#application.path#cfc/eval_faculty
I have couple resource DLLs that I currently load when application starts using following
I have couple of statements, the pseudo code would look something like this: insert
I have a complex .NET Remoting server app that provides a couple of services.
I have a complex build process to generate a couple of python extension modules
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
We have couple different web-apps that share the same db link. The hibernate layer
ok I have couple of .NET classes that I want to use in VBA.
I have inherited couple of .Net (C#) application which does not have any tracing
I have a couple of tables which look like this Table 1 user_id |

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.