Skip to content

Get an exception if the "type of class's attribute" is short. #4

@Anwar-Hamzah

Description

@Anwar-Hamzah

System.ArgumentException: 'Method 'Boolean Equals(Int32)' declared on type 'System.Int32' cannot be called with instance of type 'System.Int16''

       public class Test {
              public short RowID { get; set; }
              public string FullName { get; set; } = null!;
       }

        IList<ExpressionInput> query = QueryHelper.SpecificationEvalutorQueryable<Field>(new List<ExpressionInput>
        {
            new ExpressionInput
            {
                Operand = Operand.And, //First Item does not matter And or OR
                Operation = Operation.Equals,
                PropertyName = "FullName",
                Value = "Test1"
            },
            new ExpressionInput
            {
                Operand = Operand.And,
                Operation = Operation.Equals,
                PropertyName = "RowID",
                Value = 10
            } });

       var expressionList = ExpressionInputGenerator.GetExpressionInputList();
        var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Test>(expressionList);

so I tried to convert the value into the correct Type but not working

    private static Expression GetExpression<T>(ParameterExpression param, ExpressionInput filter)
    {
            ...

            Type type = Expression.Property(param, filter.PropertyName).Type;
            var converted = Expression.Convert(constant, type);

            switch (filter.Operation)
            {
                case Operation.Equals:
                    return Expression.Call(member, equalFilterMethod, converted);
            }
            ...
    }

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