I'm attempting to use reflection to determine the result of a call to .Equals on two different but "equal" instances of a type.
My method would be something like:
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
}
By way of example:
string a = "Test";
string b = "Test";
bool areEqual = a.Equals(b); // areEqual will be true
// so:
TypeComparesProperties(typeof(string)); // should return true
However, given:
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
If I implemented IEquatable<MyComplexType>
on my class as follows, I'd get true instead:
public class MyComplexType : IEquatable<MyComplexType>
{
public int Id { get; set; }
public string MyString { get; set; }
public bool Equals(MyComplexType other)
{
return (Id.Equals(other.Id) && MyString.Equals(other.MyString));
}
}
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.
Any other ideas?
Edit:
It seems that people are confused by my intentions. I apologise. Hopefully this will clarify:
I have a method which should compare two objects to the best of its abilities. Simply calling .Equals() won't suffice, because either:
- The objects will be value types or will implement IEquatable in a nice way and I'll get a true response. Great!
- The objects may have all the same properties and be "equal", but because they're different instances, I'll get a false response. I can't tell whether this false is because the objects are not "equal", or because they're just different instances.
So in my mind, the comparison method should:
- Examine the object type to see whether it's
Equals
method will return true for two different instances with the same public properties. - If so, call the
Equals
method and return the result - 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
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.
Edit 2:
I'm gonna close this for a couple of reasons:
- The more I talk about it, the more I realise that what I asked isn't what I really wanted to do
- 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.
Thanks all for your input.