ReadableExpressions
ReadableExpressions copied to clipboard
ArgumentException with nullable Enum
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)
+1, The same problem or you can get it when usу LINQ All, Any ...
.All(i => i.Type == SomeType.TypeOne) and property is nullable
Darnit - this is a regression introduced by #129 - I'll fix this and #133 asap.
Hi! When are you going to merge the fix? Thanks.
Hi @EvgenyMarchuk! I'm still working on processing captured values correctly and trying to nail down how that feature behaves in a wider sense. Hoping to have it sorted out soon, along with #135
This is fixed as of v4.1.2, which is now available on NuGet. Cheers!