Reputation: 49
I have a class defined as:
public class TableInfo
{
public int item_id { get; set:}
public string item_name { get; set;}
// plus several more
}
I create two instances of this class filled with different information and want to compare them. I've been doing it the hard way:
if(table1[index1].item_id == table2[index2].item_id)
//report passed
else
//report failed
and then do that again for each element in the class.
if(table1[index1].item_name == table2[index2].item_name)
and so on.
Is there a better way to handle this so that I don't have to go through a unique comparison of each individual element. It seems to me that a foreach could do it but I'm not sure how to get a list of the properties and iterate through them.
Upvotes: 1
Views: 307
Reputation: 13
If you're just comparing two items, you can use the Type.GetType().GetProperties() method, which returns an array of all of the public properties in the object. From here you can use your foreach loop to iterate through each property on the type and compare your fields.
https://msdn.microsoft.com/en-us/library/aky14axb%28v=vs.110%29.aspx
Upvotes: 0
Reputation: 1
Probably what you might want to do is implement IComparable on your TableInfo class. Here are some links with some information on implementing IComparable.
https://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx IComparable and Equals()
Upvotes: 0
Reputation: 736
You can solve it with Reflection Just showing another way to do it, but implementing equality comparison would be cleaner
this snippet will only compare public properties
static bool IsFullyEqual(object obj1, object obj2)
{
if (obj1.GetType() != obj2.GetType()) return false;
bool result = true;
foreach (var property in obj1.GetType().GetProperties())
{
object obj1Value = property.GetMethod.Invoke(obj1, null);
object obj2Value = property.GetMethod.Invoke(obj2, null);
if( obj1Value.GetHashCode()!= obj2Value.GetHashCode())
{
result = false;
break;
}
}
return result;
}
Upvotes: 0
Reputation: 24916
You can implement equality comparison of the class and then compare it using Equals
:
public class TableInfo
{
public int item_id { get; set;}
public string item_name { get; set;}
public override bool Equals(object obj)
{
if(obj == null)
return false;
if (ReferenceEquals(obj, this))
return true;
if (obj.GetType() != this.GetType())
return false;
var rhs = obj as TableInfo;
return item_id == rhs.item_id && item_name == rhs.item_name;
}
public override int GetHashCode()
{
return item_id.GetHashCode() ^ item_name.GetHashCode();
}
// Additionally you can overload == and != operators:
public static bool operator ==(TableInfo x, TableInfo y)
{
return object.Equals(x, y);
}
public static bool operator !=(TableInfo x, TableInfo y)
{
return !object.Equals(x, y);
}
}
Then instead of using
if(table1[index1].item_id == table2[index2].item_id)
you can use
if(table1[index1].Equals(table2[index2]))
Or if operators are overloaded you can use
if(table1[index1] == table2[index2])
Upvotes: 4