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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:31:29+00:00 2026-05-27T15:31:29+00:00

I have this method called MatchNodes: IEnumerable<bool> MatchNodes<T>(T n1, T n2) Which basically gets

  • 0

I have this method called MatchNodes: IEnumerable<bool> MatchNodes<T>(T n1, T n2)

Which basically gets every property and field from both T objects (via reflection, and not including properties/fields from base classes) and compares them, returning the result as a IEnumerable of bools.

When it finds a primitive type or string, if just returns the == between them.

When it finds a type derived from a collection, it iterates each member and calls MatchNodes for each of them (ouch).

When it finds any other type, it calls MatchNodes for each property/field.

My solution is obviously asking for a stack overflow exception, but I don’t have a clue on how make it better, because I have no idea how deep the objects will go.

Code (try not to cry please, it’s ugly as hell):

public static IEnumerable<bool> MatchNodes<T>(T n1, T n2)
    {
        Func<PropertyInfo, bool> func= null;

        if (typeof(T) == typeof(String))
        {
            String str1 = n1 as String;
            String str2 = n2 as String;
            func = new Func<PropertyInfo, bool>((property) => str1 == str2);
        }
        else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(typeof(T)))
        {
            System.Collections.IEnumerable e1 = (System.Collections.IEnumerable)n1;
            System.Collections.IEnumerable e2 = (System.Collections.IEnumerable)n2;
            func = new Func<PropertyInfo, bool>((property) =>
            {
                foreach (var v1 in e1)
                {
                    if (e2.GetEnumerator().MoveNext())
                    {
                        var v2 = e2.GetEnumerator().Current;
                        if (((IEnumerable<bool>)MatchNodes(v1, v2)).All(b => b == true))
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (e2.GetEnumerator().MoveNext())
                {
                    return false;
                }
                else return true;
            });
        }
        else if (typeof(T).IsPrimitive || typeof(T) == typeof(Decimal))
        {
            func = new Func<PropertyInfo, bool>((property) => property.GetValue(n1, null) == property.GetValue(n2, null)); 
        }
        else
        {
            func = new Func<PropertyInfo, bool>((property) =>
                    ((IEnumerable<bool>)MatchNodes(property.GetValue(n1, null),
                    property.GetValue(n2, null))).All(b => b == true));
        }

        foreach (PropertyInfo property in typeof(T).GetProperties().Where((property) => property.DeclaringType == typeof(T)))
        {
            bool result =func(property);
            yield return result;
        }

    }

What I’m looking at is a way to crawl into the objects without calling my method recursively.

EDIT

To clarify, example:

public class Class1 : RandomClassWithMoreProperties{
    public string Str1{get;set;}
    public int Int1{get;set;}
}

public class Class2{
    public List<Class1> MyClassProp1 {get;set;}
    public Class1 MyClassProp2 {get;set;}
    public string MyStr {get;set;}
}

MatchNodes(n1,n2) where n1.GetType() and n2.GetType() are Class2 would return true if:

  • Every Class1 object inside MyClassProp1 has the same Str1,Int1 for both objects
  • MyClassProp2 has the same Str1,Int1 for both objects
  • MyStr is equal for both objects

And I won’t compare any properties from RandomClassWithMoreProperties.

  • 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-27T15:31:30+00:00Added an answer on May 27, 2026 at 3:31 pm

    You can use a stack or queue to store the properties you want to compare. It goes along these lines:

     var stack = new Stack<Tuple<object, object>>();
    
     // prime the stack
     foreach (var prop in n1.GetType().GetProperties())
     {
         stack.Push(Tuple.Create(prop.GetValue(n1), prop.GetValue(n2));
     }
    
     while (stack.Count > 0)
     {
         var current = stack.Pop();
    
         // if current is promitive: compare
         // if current is enumerable: push all elements as Tuples on the stack
         // else: push all properties as tuples on the stack
     }
    

    If you use a Queue instead of a Stack you get a BFS instead of a DFS. Also you should probably keep track of already visited nodes in a HashSet. You also might want to add a check to make sure the types of n1 and n2 are the same.

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

Sidebar

Related Questions

I have this method which gets called from a different class: -(void)artworkInfo:(NSNumber *)pos{ [image
I have this method Verify_X which is called during databind for a listbox selected
I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
I have an interface called Dictionary which has a method insert() . This interface
I have this method, that will be called against from a WCF Client, but
I have a method called -(IBAction)setPicker:(id) sender Basically this method substitutes a keyboard into
I have a model which has a method called 'has_voted'. It looks like this...
I have this method in an exterior class that gets called whenever the charachter
I have a method called BindItems() and this method gets the values to a
So I have a method in a reservation model called add_equip. This method does

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.