Skip to content

ArgumentException with nullable Enum #134

Closed
@vlm---

Description

@vlm---

Hello, version 4.1.1 indroduced some new bug with expressions containing nullable enums:

using System;
using System.Linq.Expressions;
using AgileObjects.ReadableExpressions;

public class Program
{
	public static void Main()
	{
		Expression<Func<Product, bool>> exp = p => p.Level == ProductLevel.Classic;
		Console.WriteLine(exp.ToReadableString());
	}

	public enum ProductLevel
	{
		Classic,
		Premium
	}

	public class Product
	{
		public ProductLevel? Level { get; set; }
	}
}
System.ArgumentException: Type provided must be an Enum. (Parameter 'enumType')
   at System.Enum.ValidateRuntimeType(Type enumType)
   at System.Enum.TryParse(Type enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, Object& result)
   at System.Enum.Parse(Type enumType, String value)
   at AgileObjects.ReadableExpressions.Translations.BinaryTranslation.GetEnumValue(Expression expression, Type enumType)
   at AgileObjects.ReadableExpressions.Translations.BinaryTranslation.TryGetEnumComparisonExpression(BinaryExpression& comparison)
   at AgileObjects.ReadableExpressions.Translations.BinaryTranslation.For(BinaryExpression binary, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Extensions.PublicTranslationContextExtensions.GetCodeBlockTranslationFor(ITranslationContext context, Expression expression)
   at AgileObjects.ReadableExpressions.Translations.LambdaTranslation..ctor(LambdaExpression lambda, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslation()
   at AgileObjects.ReadableExpressions.ExpressionExtensions.ToReadableString(Expression expression, Func`2 configuration)
   at Program.<<Initialize>>d__0.MoveNext() in :line 11

Example works perfectly with version 4.1.0:
p => ((int?)p.Level) == ((int?)Program.ProductLevel.Classic)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions