How to use "Reflection "to compare classes?
-
hi i'd like to compare 2 instances of the same class. i've used the XmlSerializer to save an instance of a class into a Xml-File. after the deserialization from the file, i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same. i've ask a friend and he told me that i have to use Reflection, but he didn't told me how exactlly i have to do that. pls help greets pdluke
-
hi i'd like to compare 2 instances of the same class. i've used the XmlSerializer to save an instance of a class into a Xml-File. after the deserialization from the file, i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same. i've ask a friend and he told me that i have to use Reflection, but he didn't told me how exactlly i have to do that. pls help greets pdluke
pdluke wrote:
i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same.
Be careful. The two objects will not be the same. What you mean is that you are testing to see if they are equal. You can do this without reflection. You can override the Equals method:
public override bool Equals(object obj)
{
bool isEqual = base.Equals(obj);
if (!isEqual)
return false;// Do more stuff to determine equality - updating isEqual return isEqual;
}
Only you can determine what constitutes equality.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
pdluke wrote:
i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same.
Be careful. The two objects will not be the same. What you mean is that you are testing to see if they are equal. You can do this without reflection. You can override the Equals method:
public override bool Equals(object obj)
{
bool isEqual = base.Equals(obj);
if (!isEqual)
return false;// Do more stuff to determine equality - updating isEqual return isEqual;
}
Only you can determine what constitutes equality.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
if you have unit tests for your object you could use them instead of the equals method. It depends whether you are interested in them being functionally identical or having the same state or both. Russell
-
hi i'd like to compare 2 instances of the same class. i've used the XmlSerializer to save an instance of a class into a Xml-File. after the deserialization from the file, i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same. i've ask a friend and he told me that i have to use Reflection, but he didn't told me how exactlly i have to do that. pls help greets pdluke
Use
this.GetType().GetProperties()
to get an array of the properties, then us theGetValue
method of each to get the value of the property forthis
andobj2
to compare the values. If you're not exposing all your data as properties, useGetMembers
instead.-- Rules of thumb should not be taken for the whole hand.