二进制表达式类在比较运算符上引发无效操作异常
我正在修改一个开源程序来为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将输入解析为
long
而不是int
。You need to parse the input as a
long
rather than anint
.