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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:53:16+00:00 2026-05-25T05:53:16+00:00

What is the best way to compare two arbitrary dynamic objects for equality? For

  • 0

What is the best way to compare two arbitrary dynamic objects for equality? For example these two objects.

I.e.

dynamic obj1 = new ExpandoObject();
obj1.Name = "Marcus";
obj1.Age = 39;
obj1.LengthInMeters = 1.96;

dynamic obj2 = AMethodReturningADynamic();
obj2.Name = "Marcus";
obj2.Age = 39;
obj2.LengthInMeters = 1.96;

Assert.AreEqual(obj1, obj2); // ?

Or is there a way to get the actual properties and their values as lists? To create an ExpandoObject from a dynamic type for example?

  • 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-25T05:53:16+00:00Added an answer on May 25, 2026 at 5:53 am

    The Microsoft API’s for dynamically invoking methods and propertys on arbitrary dynamic objects (IDynamicMetaObjectProvider) are not easy to use when you don’t have the compiler’s help. You can use Dynamitey (via nuget) to simplify this completely. It has a static function Dynamic.InvokeGet to call property’s getters with just a target and a property name.

    To get a list of properties of the dynamic object, there is a bit of a gotcha, as the dynamic object has to support it (if it’s a DynamicObject that means implementing GetDynamicMemberNames, Expando supports it, but random IDynamicMetaObjectProvider may not and just return an empty list). Dynamitey has a method to simplifying getting those names as well, Dynamic.GetMemberNames.

    Both of those two functions give you the basic tools necessary to compare many arbitrary dynamic objects via properties.

    //using System.Dynamic;
    //using Dynamitey;
    //using System.Linq;
    
    IEnumerable<string> list1 =Dynamic.GetMemberNames(obj1);
    list1 = list1.OrderBy(m=>m);
    IEnumerable<string> list2 =Dynamic.GetMemberNames(obj2);
    list2 = list2.OrderBy(m=>m);
    
    if(!list1.SequenceEqual(list2))
     return false;
    
    foreach(var memberName in list1){
     if(!Dynamic.InvokeGet(obj1, memberName).Equals(Dynamic.InvokeGet(obj2,memberName))){
        return false;
     }
    }
    return true;
    

    However, if they are just your own DynamicObject subclass then it’d be easier to just follow the typical rules for implementing Equals, there really is no difference from non-dynamic objects, and just compare what you are internally using for state.

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

Sidebar

Related Questions

I want to know the best way to compare two objects and to find
What is the best way to compare objects in JavaScript? Example: var user1 =
what is the best way to compare two objects and find the differences? Customer
What's the best way to compare two queries by two columns? these are my
What's the best way in code to compare enum values? For example, if I
Possible Duplicate: What is the best way to compare two entity framework entities? I
What would be the best way to compare two date in java? Right now
What is the best way to compare two strings to see how similar they
Say I have these two DateTime objects: var dt1 = new DateTime(1900,12,1,1,1,1); var dt2
What's the best way to compare two time zones? I'm facing an issue while

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.