Skip to content

ToString() on Nullable<TEnum> generates an invalid Expression.Call #59

@wassim-k

Description

@wassim-k

This is a fairly edge case, since I think the correct expression is probably Trend?.ToString() but thought I'd let you know since it's a regression from Projectables.

using ExpressiveSharp.Mapping;

// ExpressiveSharp 0.7.0: a switch-style conditional that builds a string
// from a Nullable<TEnum> via .ToString() generates an invalid Expression.Call
// node — the constant operand is typed as the unwrapped TEnum, but the
// MethodInfo passed is Nullable<TEnum>.ToString. The Expression API rejects
// this:
//
//   System.ArgumentException: Method 'System.String ToString()' declared on
//   type 'System.Nullable`1[Trend]' cannot be called with instance of type
//   'Trend'
//
// Generated _Reading.TrendLabel.g.cs (line 25-26):
//
//   var expr_6 = Expression.Constant(Trend.NoChange, typeof(Trend));      // unwrapped
//   var expr_7 = Expression.Call(expr_6,
//                   typeof(Trend?).GetMethod("ToString", ...),            // Nullable<Trend>.ToString
//                   Array.Empty<Expression>());                            // → throws

ExpressiveSharp.Generated.ExpressionRegistry.TryGet(typeof(Reading).GetProperty(nameof(Reading.TrendLabel))!);

public partial class Reading
{
    public Trend? Trend { get; init; }

    [ExpressiveProperty("TrendLabel")]
    private string TrendLabelExpr => Trend.ToString() ?? string.Empty;
}

public enum Trend { Increase, Decrease, NoChange }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions