Beef icon indicating copy to clipboard operation
Beef copied to clipboard

Enum.Parse fails intermittently when using Object

Open Grimelios opened this issue 5 months ago • 0 comments

static
{
    public static void Main()
    {
        Object obj = MyEnum.Entry1;

        let result = Enum.Parse(obj.GetType(), "Entry1");

        // The line above fails to parse (this line prints "Err()")
        Console.WriteLine(result);
    }
}

public enum MyEnum
{
    Entry1,
    Entry2,
    Entry3,
}

I'm running 07/20/2025. As commented, the code above fails to parse a MyEnum value, which is erroneous behavior since the value Entry1 does exist within the enumeration. What's stranger, however, is that the following code does successfully parse:

static
{
    public static void Main()
    {
        Object obj = MyEnum.Entry1;

        let result1 = Enum.Parse(typeof(MyEnum), "Entry1");
        let result2 = Enum.Parse(obj.GetType(), "Entry1");

        // With the newly-added parse (result1) that explicitly uses typeof(MyEnum), both lines successfully parse, printing "Ok(0)".
        Console.WriteLine(result1);
        Console.WriteLine(result2);
    }
}

public enum MyEnum
{
    Entry1,
    Entry2,
    Entry3,
}

Important note: if the second example still fails to parse, clean the project first. That's why I used the word "intermittent" in this issue's title.

I have a second example of the same problem. Begin with the original example (which fails to parse), then modify by adding a single line, as shown:

static
{
    public static void Main()
    {
        Object obj = MyEnum.Entry1;

        let b = obj.GetType() == typeof(MyEnum);
        let result = Enum.Parse(obj.GetType(), "Entry1");

        // With the newly-added line that, again, explicitly uses typeof(MyEnum), the parse succeeds.
        Console.WriteLine(result);
    }
}

public enum MyEnum
{
    Entry1,
    Entry2,
    Entry3,
}

This new, successful parse is similarly intermittent. Sometimes the parse succeeds right away (i.e. on the next program run), but sometimes a second run or a project clean is required.

Grimelios avatar Jul 22 '25 00:07 Grimelios