hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
522 stars 62 forks source link

.net 8 or vs || #221

Closed PoKNicolas closed 1 month ago

PoKNicolas commented 8 months ago

With .net 8 RC2 the "or" causes an exception when decompile, while "||" works. With .net 7 "or" and "||" both works fine.

Unhandled exception. System.InvalidOperationException: The binary operator LessThanOrEqual is not defined for the types 'ConsoleApp1.TestEnum' and 'System.Int32'.

Here is some sample code:

public class TestClass
{
    public Guid Id { get; set; }
    public TestEnum Enum { get; set; }

    // will throw
    [Computed] public bool TestWithOr => Enum is TestEnum.A or TestEnum.B;
    // works
    [Computed] public bool TestWithPipes => Enum is TestEnum.A || Enum is TestEnum.B;
}

public enum TestEnum
{
    A,B,C
}

and try to decompile

using var db = new TestContext();

        var testPipes = db.TestClasses.Where(x => x.TestWithPipes).Decompile().FirstOrDefault();
        Console.WriteLine("Test with pipes woks.");

        // this will fail
        var testOr = db.TestClasses.Where(x => x.TestWithOr).Decompile().FirstOrDefault();
        Console.WriteLine("Test with or woks.");
hazzik commented 7 months ago

That is interesting. .NET 8 optimized the condition to:

    public bool TestWithOr
    {
        get
        {
            TestEnum @enum = Enum;
            if ((uint)@enum <= 1u)
            {
                return true;
            }
            return false;
        }
    }