Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there an effective way to determine whether .Equals on two different but "equal" instances will return true?
    primarykey
    data
    text
    <p>I'm attempting to use reflection to determine the result of a call to .Equals on two different but "equal" instances of a type.</p> <p>My method would be something like:</p> <pre><code>public static bool TypeComparesProperties(Type t) { // return true if (an instance of t).Equals(a different instance of t) // will be true if all publicly accessible properties of the instances // are the same } </code></pre> <p>By way of example:</p> <pre><code>string a = "Test"; string b = "Test"; bool areEqual = a.Equals(b); // areEqual will be true // so: TypeComparesProperties(typeof(string)); // should return true </code></pre> <p>However, given:</p> <pre><code>public class MyComplexType { public int Id { get; set; } public string MyString { get; set; } } MyComplexType a = new MyComplexType {Id = 1, MyString = "Test"}; MyComplexType b = new MyComplexType { Id = 1, MyString = "Test" }; bool areEqual = a.Equals(b); // areEqual will be false // so: TypeComparesProperties(typeof(MyComplexType)); // should return false </code></pre> <p>If I implemented <code>IEquatable&lt;MyComplexType&gt;</code> on my class as follows, I'd get true instead:</p> <pre><code>public class MyComplexType : IEquatable&lt;MyComplexType&gt; { public int Id { get; set; } public string MyString { get; set; } public bool Equals(MyComplexType other) { return (Id.Equals(other.Id) &amp;&amp; MyString.Equals(other.MyString)); } } </code></pre> <p>I figure I can probably do it by instantiating two instances using reflection, then setting all properties to appropriately typed default values. That's a lot of work though and a lot of overhead, and I think I'd run into problems if there was no empty constructor on the type.</p> <p>Any other ideas?</p> <hr> <p><strong>Edit:</strong></p> <p>It seems that people are confused by my intentions. I apologise. Hopefully this will clarify:</p> <p>I have a method which should compare two objects to the best of its abilities. Simply calling .Equals() won't suffice, because either:</p> <ol> <li>The objects will be value types or will implement IEquatable in a nice way and I'll get a true response. Great!</li> <li>The objects may have all the same properties and be "equal", but because they're different instances, I'll get a false response. <strong>I can't tell whether this false is because the objects are not "equal", or because they're just different instances</strong>.</li> </ol> <p>So in my mind, the comparison method should:</p> <ol> <li>Examine the object type to see whether it's <code>Equals</code> method will return true for two different instances with the same public properties.</li> <li>If so, call the <code>Equals</code> method and return the result</li> <li>If not, have a look at all the properties and fields and compare them as well as I can to determine whether they're equal</li> </ol> <p>Now, I understand that I could just skip to step 3, but if there's a way to determine ahead of time whether it's necessary, that'd be a time-saver.</p> <hr> <p><strong>Edit 2:</strong></p> <p>I'm gonna close this for a couple of reasons:</p> <ol> <li>The more I talk about it, the more I realise that what I asked isn't what I really wanted to do</li> <li>Even if I did want to do this, there's no real shortcut at all. RE the earlier edit, I should just skip to step 3 anyway.</li> </ol> <p>Thanks all for your input.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload