Assert.AreEqual to not fail when comparing an enum and an int

Posted by codingbear on Stack Overflow See other posts from Stack Overflow or by codingbear
Published on 2010-06-01T21:48:06Z Indexed on 2010/06/01 21:53 UTC
Read the original article Hit count: 492

Filed under:
|

I'm not sure if this is doable, but I will just give a shot.

I am calling Assert.AreEqual() method. For the parameters, I'm passing...

  1. an enum value which has Int32 as the underlying type because I didn't specify the base type
  2. an int (Int32) value

Assert fails because it sees that the enum is not int (which is definitely correct).
However, is there a way to make this Assert pass when the enum has the correct int value as the 2nd parameter?

I can cast the enum to int and have it a quick fix, but it's really ugly.
I was expecting some kind of overriding a method that Assert uses to compare 2 different objects and implicitly make that enum type look like an int. However, I wasn't successful at finding any hint/answer around that so far.

Someone suggested to create a type converter and use the TypeConverterAttribute to get around. If this works for sure and is the only way to do it, I would; however, it does seem a lot of unnecessary work.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET