0

Are there any libraries/frameworks for .net that would take two objects of the same type and using reflection match values of all the properties?

I had to compare two objects having 2 Nullable DateTime properties and the code lookes ugly as hell:

private bool SameValues(ExpiryDates ExpiryDates1, ExpiryDates ExpiryDates2)
{
        //Assume they are the same value and then look for differences
        bool result = true;

        if (ExpiryDates1.PSL_ExpiryDate.HasValue != ExpiryDates2.PSL_ExpiryDate.HasValue)
        {
            result = false;
        }

        if (ExpiryDates1.MNL_ExpiryDate.HasValue != ExpiryDates2.MNL_ExpiryDate.HasValue)
        {
            result = false;
        }

        if ((ExpiryDates1.MNL_ExpiryDate != null) && (ExpiryDates2.MNL_ExpiryDate != null))
            if (ExpiryDates1.MNL_ExpiryDate.Value != ExpiryDates2.MNL_ExpiryDate.Value)
                result = false;

        if ((ExpiryDates1.PSL_ExpiryDate != null) && (ExpiryDates2.PSL_ExpiryDate != null))
            if (ExpiryDates1.PSL_ExpiryDate.Value != ExpiryDates2.PSL_ExpiryDate.Value)
                result = false;

        return result;
}
Andrei V
  • 7,306
  • 6
  • 44
  • 64
jimjim
  • 2,414
  • 2
  • 26
  • 46
  • @Adriano Repetti : Very nice answer indeed, never thought of using serialization for this purpose, just amazing! – jimjim Jul 01 '15 at 06:36
  • 1
    Thanks but also see my comment in the other answer. It's easy but slow and binary vs logical equivalence may be tricky. 1.0 == 1.0f? NaN == NaN? What about conversion operators and custom comparison? You may use it but in very specific and limited scenarios. Otherwise do it by hand (2nd method in that answer) or use a library (Pranab's answer) – Adriano Repetti Jul 01 '15 at 06:42

1 Answers1

1

There is no library avaiable as per my knowldege but in .NEt framework for comparison of same type of object we have IComparare interface that you can use and do comparison between two object of same type.

public class BoxComp : IComparer<Box>
{
    // Compares by Height, Length, and Width. 
    public int Compare(Box x, Box y)
    {
       ///you code to do comparison
    }
}

you can aslo make it generic.

You can check this : Compare .NET Objects but i suggest to go for comparare impmentation as there is no big need as you just want to compare datetime of two object.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • I want to be able to easily compare two objects of the same type having same values, without having to write IComparer for each type. There must be an easier way to do this with reflection somehow. – jimjim Jul 01 '15 at 06:26
  • 1
    @Arjang - i suggest to make use of generic for that or check the codeplex link given by me in answer – Pranay Rana Jul 01 '15 at 06:33
  • @Pranya Rana : The link to another question shows using serialiszation as a solution, I like that approach much better than what I was looking for. – jimjim Jul 01 '15 at 06:37
  • yes it looks fine... – Pranay Rana Jul 01 '15 at 07:20
  • @Pranya Rana : but it seems to have it's own problems, guess I have to use this approach to be explicit about what makes a difference and look for those only, instead of any variation in values being considered a difference – jimjim Jul 01 '15 at 07:23