Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompiling throws InvalidOperationException for enums with underlying type other than int #96

Open
gvas opened this issue Jan 6, 2017 · 2 comments · May be fixed by #208
Open

Decompiling throws InvalidOperationException for enums with underlying type other than int #96

gvas opened this issue Jan 6, 2017 · 2 comments · May be fixed by #208

Comments

@gvas
Copy link
Contributor

gvas commented Jan 6, 2017

.NET Framework 4.5.2
DelegateDecompiler 0.21.0

enum PayGrade : byte
{
    Low,
    High
}

class Employee
{
    public PayGrade PayGrade { get; set; }

    [Computed]
    public decimal Salary
    {
        get
        {
            if (PayGrade == PayGrade.High)
            {
                return 1000M;
            }

            return 10M;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var employees = new[]
        {
            new Employee { PayGrade = PayGrade.Low },
            new Employee { PayGrade = PayGrade.High },
        }
        .AsQueryable();

        var salaries = (from employee in employees
                        select employee.Salary)
                        .Decompile()
                        .ToList();
    }
}

The code above throws an InvalidOperationException with the following message:
"The binary operator Equal is not defined for the types 'System.Byte' and 'System.Int32'."

Changing the PayGrade enum's underlying type to int prevents the exception.

@gvas gvas changed the title Enums with underlying type other than int throw InvalidOperationException Decompiling throws InvalidOperationException for enums with underlying type other than int Jan 6, 2017
@hazzik hazzik added the bug label Jan 10, 2017
hazzik added a commit that referenced this issue Mar 13, 2017
hazzik added a commit that referenced this issue Mar 13, 2017
@qszhuan
Copy link

qszhuan commented Jan 28, 2018

any updates for this bug?
I have a similar bug when I mixed the enum comparison and other property into one method/property.

[Computed] private static bool B(Order x) { return x.IsActive && (x.OrderStatus >= OrderStatus.Complete|| x.OrderStatus == OrderStatus.Withdraw); }

The exception is:
The binary operator GreaterThanOrEqual is not defined for the types 'OrderStatus' and 'System.Int32'.

The ORM is Fluent NHibernate.

@gvas
Copy link
Contributor Author

gvas commented Jan 29, 2018

I have a workaround, but I'm not sure if it is applicable in your case. Try to add an additional computed property which explicitly casts the enum to int.

[Computed]
public int OrderStatusAsInt
{
    get { return (int)OrderStatus; }
}

[Computed]
public bool B
{
    get { return IsActive && (OrderStatusAsInt >= OrderStatus.Complete || OrderStatusAsInt == OrderStatus.Withdraw); }
}

(I've converted your B method to a property simply because I'm not familiar with computed methods.)

hazzik added a commit that referenced this issue Feb 5, 2021
hazzik added a commit that referenced this issue Feb 6, 2021
@hazzik hazzik linked a pull request Oct 17, 2022 that will close this issue
hazzik added a commit that referenced this issue Oct 17, 2022
@hazzik hazzik linked a pull request Oct 17, 2022 that will close this issue
hazzik added a commit that referenced this issue May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants