0

Here is the situation. I have two unknown type's objects that i would like to compare to know if one is identical to another. Both can be string, int, enumerable or any custom class you can imagine. Is there a way to achieve this using reflection ?

Thanks !

user2161442
  • 21
  • 1
  • 5
  • They should be, if they are not, i would say they aren't equal – user2161442 Apr 25 '13 at 17:49
  • Could you use the [is keyword](http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.71).aspx)? Could you maybe do `if (a is b)`? I don't know if this works off the top of my head, worth a try though – tnw Apr 25 '13 at 17:50
  • Why do you need "using reflection"? Servy's answer (+1) shows how to do that without reflection. Or you want manually compare each field of both objects via reflection (dangerous idea IMHO). – Alexei Levenkov Apr 25 '13 at 17:51

3 Answers3

0

You use the Equals method from object, in the general case.

object first = GetFirst();
object second = GetSecond();

bool areEqual = object.Equals(first, second);

Another option when writing a particular function (often a generic function) that needs to compare types not known at compile time is to accept an IEqualityComparer, so that if the type doesn't have an implementation of Equals that's appropriate in context the caller of your method (or user of your type, if it's for the whole class) can provide their own implementation. A good example of this is a method such as string.Contains. There is an optional overload that accepts an IEqualityComparer<char> that lets the caller define what it means for two characters to be equal.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • this will not work, i can't compare two objects of a custom type by using the object class equals method.. – user2161442 Apr 25 '13 at 17:51
  • 1
    @user2161442 Sure you can, if the class uses a sensible `Equals` implementation (which objects that you would expect to be compared in this manor should do). If it doesn't have one, see the edit. – Servy Apr 25 '13 at 17:53
0

If your class overrides Equals, use Servy's approach. Otherwise, you can use CompareNetObjects library (available on nuget).

Here is an example of comparing two objects by their public properties.

Community
  • 1
  • 1
tukaef
  • 9,074
  • 4
  • 29
  • 45
0

Use Object.ReferenceEquals and GetType

Example pulled from MSDN:

int n1 = 12;
int n2 = 82;
long n3 = 12;

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()));
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()));
// The example displays the following output: 
//       n1 and n2 are the same type: True 
//       n1 and n3 are the same type: False    

Source:http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx

I may have misunderstood, however. This will check only check to see if they are the same type, two objects of the same type with different references will still evaluate to true using this method.

tnw
  • 13,521
  • 15
  • 70
  • 111