6

I have this line of C# code:

var __allFlags = Enum.Parse(enumType, allFlags);

It is throwing an InvalidCastException and I can't figure out why - if I set a breakpoint and run Enum.Parse(enumType, allFlags) in the watch window, I get the expected result and not an error.

enumType is set to typeof(PixelColor) where PixelColor is an enum I am using for unit testing purposes, and allFlags is set to the string "Red" which is one of the possible values of PixelColor.

edit: here is my unit test:

[TestMethod]
public void IsFlagSetStringTest()
{
    Assert.IsTrue(EnumHelper.IsFlagSet(typeof(PixelColor), "Red", "Red"));
    Assert.IsFalse(EnumHelper.IsFlagSet(typeof(PixelColor), "Red", "Green"));
    Assert.IsTrue(EnumHelper.IsFlagSet(typeof(PixelColor), "White", "Red"));
    Assert.IsTrue(EnumHelper.IsFlagSet(typeof(PixelColor), "White", "Red, Green"));
    Assert.IsFalse(EnumHelper.IsFlagSet(typeof(PixelColor), "Red", "Red, Green"));
}

and here is the method being tested:

/// <summary>
/// Determines whether a single flag value is specified on an enumeration.
/// </summary>
/// <param name="enumType">The enumeration <see cref="Type"/>.</param>
/// <param name="allFlags">The string value containing all flags.</param>
/// <param name="singleFlag">The single string value to check.</param>
/// <returns>A <see cref="System.Boolean"/> indicating that a single flag value is specified for an enumeration.</returns>
public static bool IsFlagSet(Type enumType, string allFlags, string singleFlag)
{
    // retrieve the flags enumeration value
    var __allFlags = Enum.Parse(enumType, allFlags);
    // retrieve the single flag value
    var __singleFlag = Enum.Parse(enumType, singleFlag);

    // perform bit-wise comparison to see if the single flag is specified
    return (((int)__allFlags & (int)__singleFlag) == (int)__singleFlag);
}

and just in case, here is the enum used for testing:

/// <summary>
/// A simple flags enum to use for testing.
/// </summary>
[Flags]
private enum PixelColor
{
    Black = 0,
    Red = 1,
    Green = 2,
    Blue = 4,
    White = Red | Green | Blue
}
6
  • Are you using multi-threaded code? Sounds like a possible race condition to me on the value of allFlags. Commented Jul 19, 2019 at 18:10
  • Can you edit the question with a minimal reproducible example please? Commented Jul 19, 2019 at 18:11
  • I am running this code in a unit test; I do have the "run unit tests in parallel" option turned on. However this occurs even if I only run the one unit test that is failing here. Commented Jul 19, 2019 at 18:12
  • @ekolis need to show more code then. Show us defining and setting the variables, the enum class, etc. Commented Jul 19, 2019 at 18:13
  • can you include more of the surrounding test. It would be great to be able to have the full context including any setup that you do as well as where you set the value of allFlags. Commented Jul 19, 2019 at 18:13

1 Answer 1

3

I suspect the issue is in your bitwise comparison:

return (((int)__allFlags & (int)__singleFlag) == (int)__singleFlag);

Since Enum.Parse does not throw an InvalidCastException in any scenario, and is returning an object type.

As a debugging step, comment that line out and replace it with return true; temporarily, then run the test to see if exception is thrown. If not, you may need to explicitly cast to the enum type on the prior Parse lines before casting to int.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.