I am trying to write a method that compares the PROPERTIES of two instances of an object, with these two caveats:
- The objects will often have other complex objects as properties, or even arrays of other complex objects, and
- if a property is null on one instance of the object, don't worry if the other instance of the object has values or not.
Basically, something like this (see bottom of the code for desired results):
Car c1 = new Car{ make = "ford", model = "F-150",
options = new[]{
new Option {name = "seats", value = "leather"},
new Option {name = "radio", value = "XM"}}};
Car c2 = new Car{ make = "ford", model = "F-150"};
Car c3 = new Car{ make = "ford", model = "mustang",
options = new[]{
new Option {name = "seats", value = "leather"},
new Option {name = "radio", value = "XM"}}};
Car c4 = new Car{ make = "ford", model = "F-150",
options = new[]{
new Option {name = "seats", value = "leather"}}};
c1.MagicCompare(c2); // would return true, since all non-null properties match
c1.MagicCompare(c3); // would return false, since all non-null properties do NOT match
c1.MagicCompare(c4); // would return false, since the options array doesn't match
With these classes
public class Car
{
public string make { get; set; }
public string model { get; set; }
public Option[] options { get; set;}
public bool MagicCompare(Car obj)
{
// Do magic comparison
}
}
public class Option
{
public string name { get; set; }
public string value { get; set; }
}