How to override Equals, so you can compare two identical class with two directories without specifying them statically? At the moment, there is an object composed of the following fields. For Types String, Int32, etc. Equals satisfies the conditions.
public class RatiosAVG
{
public RatiosAVG()
{
Dict1 = new Dictionary<Int32, OtherObject1>();
Dict2 = new Dictionary<Int32, OtherObject2>();
}
public OtherObject1 Obj { get; set; }
public Dictionary<Int32, OtherObject1> Dict1 { get; set; }
public Dictionary<Int32, OtherObject2> Dict2 { get; set; }
public String Name { get; set; }
public Int32 Value { get; set; }
public override bool Equals(Object obj)
{
try
{
if (!(obj is RatiosAVG))
return false;
RatiosAVG other = (RatiosAVG)obj;
Type type = typeof(RatiosAVG);
foreach (System.Reflection.PropertyInfo property in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
Object selfValue = type.GetProperty(property.Name).GetValue(this, null);
Object otherValue = type.GetProperty(property.Name).GetValue(other, null);
if ((selfValue == null || !selfValue.Equals(otherValue)) && selfValue != otherValue)
return false;
}
if (type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Count() == 0)
return false;
else
return true;
}
catch (Exception) { return false; }
}
}
OtherObject1 and OtherObject2 is Object