How do I compare two variables of type Object?

64 Views Asked by At

The problem I am facing is that I want to compare two objects of type CustomerEntity. Sometimes both of the objects being compared match on EntityID, EntityType, and EntityName, so I need a way to compare EntityObject as well.

public class CustomerEntity 
{
  public string EntityID { get; set; }
  public string EntityName { get; set; }
  public string EntityType { get; set; }
  public object EntityObject { get; set; }
}

The value of EntityObject could be one of several different types of objects depending on what the value of EntityType is. All of the types which EntityObject could be all implement IComparable, but the basic Object class does not, so how do I go about comparing them?

Below is an example of a class which EntityObject could be:

    public class EquipmentEntity : IEquatable<EquipmentEntity>, IComparable<EquipmentEntity>
    {
        public string Manufacturer { get; set; }
        public string Model { get; set; }
        public int ModelYear { get; set; }

        public override bool Equals(object obj)
        {
            return EqualityTester(this, obj as EquipmentEntity);
        }

        public bool Equals(EquipmentEntity other)
        {
            return EqualityTester(this, other);
        }

        public override int GetHashCode()
        {
            return (Manufacturer + Model + ModelYear.ToString()).GetHashCode();
        }

        private static bool EqualityTester(EquipmentEntity a, EquipmentEntity b)
        {
            if (a.Manufacturer.ToLower().Equals(b.Manufacturer.ToLower()) == false ) { return false; }
            if (a.Model.ToLower().Equals(b.Model.ToLower()) == false ) { return false; }
            if (a.ModelYear.Equals(b.ModelYear) == false ) { return false; }
            return true;
        }

        public int CompareTo(object obj)
        {
            return ComparisonTester(this, obj as EquipmentEntity);
        }

        public int CompareTo(EquipmentEntity other)
        {
            return ComparisonTester(this, other);
        }

        private static int ComparisonTester(EquipmentEntity a, EquipmentEntity b)
        {
            if (a is null && b != null) { return -1; }
            if (a != null && b is null) { return 1; }
            if (a is null && b is null) { return 0; }
            return (a.Manufacturer + a.Model + a.ModelYear.ToString()).CompareTo(b.Manufacturer + b.Model + b.ModelYear.ToString());
        }
    }
1

There are 1 best solutions below

0
Dave Holden On

I figured it out. I created an abstract class which has abstract methods for the implementation of the IComparable (CompareTo) and IEquitable (Equals, GetHashCode) methods.

public abstract class EntityWrapper : IComparable<EntityWrapper>, IEquatable<EntityWrapper>
    {
        public abstract override bool Equals(Object obj);
        public abstract bool Equals(EntityWrapper other);
        public abstract override int GetHashCode();
        public abstract int CompareTo(Object obj);
        public abstract int CompareTo(EntityWrapper other);
    }

The actual entity object classes provide their own implementations of these methods which are called by generic Linq methods such as .Equals() and .Sort(). Hope this helps anyone else who runs into this issue.