二进制表达式类在比较运算符上引发无效操作异常

发布于 2024-11-17 15:59:03 字数 2258 浏览 0 评论 0原文

我正在修改一个开源程序来为 Silverlight 中的数据网格创建通用过滤器。该类的代码如下所示。

    public PropertyData Property { get; set; }
    public FilterOperatorType FilterOperator { get; set; }
    public string FilterValue { get; set; }

    public Expression GetExpression<T>(ParameterExpression pe)
    {
        if (Property == null || Property.PropertyName == null)
            return null;
        PropertyInfo prop = typeof(T).GetProperty(Property.PropertyName);
        Expression left = Expression.Property(pe, prop);
        Expression right = null;

        switch (prop.PropertyType.Name)
        {
            case "String":
                right = Expression.Constant(FilterValue);
                break;
            case "Int32":
                int val;
                int.TryParse(FilterValue, out val);
                right = Expression.Constant(val);
                break;
            case "Int64":  
                int.TryParse(FilterValue, out val);
                Convert.ToInt32(val);        //does not work
                right = Expression.Constant(val);
                break;
            case "DateTime":
                DateTime dt;
                DateTime.TryParse(FilterValue, out dt);
                right = Expression.Constant(dt);
                break;
        }

        switch (FilterOperator)
        {
            case FilterOperatorType.Equal:
                return Expression.Equal(left, right);
            case FilterOperatorType.GreaterThan:
                return Expression.GreaterThan(left, right);
            case FilterOperatorType.GreaterThanOrEqual:
                return Expression.GreaterThanOrEqual(left, right);
            case FilterOperatorType.LessThan:
                return Expression.LessThan(left, right);
            case FilterOperatorType.LessThanOrEqual:
                return Expression.LessThanOrEqual(left, right);
            case FilterOperatorType.NotEqual:
                return Expression.NotEqual(left, right);
        }
        return null;
    }
}

每当我尝试使用整数进行过滤时,我都会收到一个 InvalidOperationException ,其状态为: 没有为类型“System.Int64”和“System.Int32”定义二元运算符 Equal。

我明白为什么抛出这个异常,但是在这段代码的示例程序中,我没有得到任何异常,因为用户输入的整数是 Int32 类型,而在我的应用程序中它是 Int64 类型。有人对如何解决这个问题有任何想法吗?

I am modifying an open source program to create a generic filter for a datagrid in Silverlight. The code for the class is shown below.

    public PropertyData Property { get; set; }
    public FilterOperatorType FilterOperator { get; set; }
    public string FilterValue { get; set; }

    public Expression GetExpression<T>(ParameterExpression pe)
    {
        if (Property == null || Property.PropertyName == null)
            return null;
        PropertyInfo prop = typeof(T).GetProperty(Property.PropertyName);
        Expression left = Expression.Property(pe, prop);
        Expression right = null;

        switch (prop.PropertyType.Name)
        {
            case "String":
                right = Expression.Constant(FilterValue);
                break;
            case "Int32":
                int val;
                int.TryParse(FilterValue, out val);
                right = Expression.Constant(val);
                break;
            case "Int64":  
                int.TryParse(FilterValue, out val);
                Convert.ToInt32(val);        //does not work
                right = Expression.Constant(val);
                break;
            case "DateTime":
                DateTime dt;
                DateTime.TryParse(FilterValue, out dt);
                right = Expression.Constant(dt);
                break;
        }

        switch (FilterOperator)
        {
            case FilterOperatorType.Equal:
                return Expression.Equal(left, right);
            case FilterOperatorType.GreaterThan:
                return Expression.GreaterThan(left, right);
            case FilterOperatorType.GreaterThanOrEqual:
                return Expression.GreaterThanOrEqual(left, right);
            case FilterOperatorType.LessThan:
                return Expression.LessThan(left, right);
            case FilterOperatorType.LessThanOrEqual:
                return Expression.LessThanOrEqual(left, right);
            case FilterOperatorType.NotEqual:
                return Expression.NotEqual(left, right);
        }
        return null;
    }
}

Anytime I try to filter with an integer, I get an InvalidOperationException that state's:
The binary operator Equal is not defined for the types 'System.Int64' and 'System.Int32'.

I understand why this exception is being thrown, however on the example program for this code I don't get any exceptions, due to the user's inputed integer being of type Int32, while in my application it is an Int64. Anyone have any ideas on how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2024-11-24 15:59:03

您需要将输入解析为 long 而不是 int

You need to parse the input as a long rather than an int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文