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

Xamarin.Forms - DynamicExpressionParser.ParseLambda fails when comparing enum properties by their int value #479

Closed
ManuLin opened this issue Feb 1, 2021 · 8 comments
Assignees
Labels

Comments

@ManuLin
Copy link

ManuLin commented Feb 1, 2021

I am facing the following issue.

Consider the following setup:

class Foo {
    public MyEnum MyEnum1 { get; set; }
}

enum MyEnum {
    First = 0,
    Second = 1
}

When I invoke the following snippet

var lambdaParameter = new[] { Expression.Parameter(typeof(Foo), string.Empty) };

var expression = DynamicExpressionParser.ParseLambda(lambdaParameter, typeof(bool), "MyEnum = MyEnum.Second", parameters);

I obtain the correct lambda expression, however, when I'm using the associated integer values for comparison, e.g.

var lambdaParameter = new[] { Expression.Parameter(typeof(Foo), string.Empty) };

var expression = DynamicExpressionParser.ParseLambda(lambdaParameter, typeof(bool), "MyEnum = 1", parameters);

I run into an "System.ArgumentNullException: Value cannot be null. Parameter name: value" in a .NET Standard project.
System.Enum.ToObject (Type enumType, Object value) System.Linq.Dynamic.Core.Parser ExpressionPromoter.Promote (Expression expr, Type type, Boolean exact, Boolean convertExpr) System.Linq.Dynamic.Core.Parser ExpressionParser.ParseComparisonOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseLogicalAndOrOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseIn () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseAndOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseOrOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseLambdaOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseNullCoalescingOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.ParseConditionalOperator () System.Linq.Dynamic.Core.Parser ExpressionParser.Parse (Type resultType, Boolean createParameterCtor) System.Linq.Dynamic.Core DynamicExpressionParser.ParseLambda (ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values)

Interestingly everything works as expected when invoked in a project targeting the full .NET framework.

Any help is appreciated!

Best regards,

Manuel

@StefH
Copy link
Collaborator

StefH commented Feb 1, 2021

Dear @ManuLin,

Can you please provide a full (console) example project to show this error?

@ManuLin
Copy link
Author

ManuLin commented Feb 2, 2021

Dear @StefH,

Apparently the issue seems to be not when invoking this method within a .NET Standard class library or a .NET Core console application, but when invoking it from a Xamarin.Forms application. I setup a simple sample app to illustrate this issue, hope this helps. Please note: The problem is observed on all platforms supported by Xamarin.Forms.

ExpressionSample.zip

Regards,

Manuel

@StefH
Copy link
Collaborator

StefH commented Feb 2, 2021

Thanks. I'll take a look.

Did you try to name the property in the Foo-class differently?

class Foo {
    public MyEnum Abc { get; set; }
}

var lambdaParameter = new[] { Expression.Parameter(typeof(Foo), string.Empty) };

var expression = DynamicExpressionParser.ParseLambda(lambdaParameter, typeof(bool), "Abc= 1", parameters);

@ManuLin
Copy link
Author

ManuLin commented Feb 2, 2021

When I rename the property I run into a different kind of exception. Considering your sample code above I face a

System.Linq.Dynamic.Core.Exceptions.ParseException: No property or field 'MyEnum' exists in type 'Foo'.

@StefH
Copy link
Collaborator

StefH commented Feb 2, 2021

You also need "Abc= 1"

@ManuLin
Copy link
Author

ManuLin commented Feb 2, 2021

In this case I run into the same ArgumentNullException as described in my initial comment. This is also reproducible with the example I provided.

Value cannot be null. Parameter name: value

@StefH StefH changed the title DynamicExpressionParser.ParseLambda fails when comparing enum properties by their int value Xamarin.Forms - DynamicExpressionParser.ParseLambda fails when comparing enum properties by their int value Feb 2, 2021
@StefH StefH self-assigned this Feb 2, 2021
@StefH StefH added the bug label Feb 2, 2021
@StefH
Copy link
Collaborator

StefH commented Feb 2, 2021

#481

@StefH
Copy link
Collaborator

StefH commented Feb 6, 2021

Fix will be included in next version,.

@StefH StefH closed this as completed Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants