【发布时间】:2014-07-24 18:33:16
【问题描述】:
我正在尝试转换参数表达式,但在转换为值类型时遇到了问题。以下是我的代码示例:
public static MemberExpression ConvertToType(ParameterExpression sourceParameter,
PropertyInfo propertyInfo,
TypeCode typeCode)
{
var sourceExpressionProperty = Expression.Property(sourceParameter, sourceProperty);
//throws an exception if typeCode is a value type.
Expression convertedSource = Expression.Convert(sourceExpressionProperty,
Type.GetType("System." + typeCode));
return convertedSource;
}
我得到以下无效操作异常:
No coercion operator is defined between types 'System.String' and 'System.Decimal'.
任何有关此转换的帮助将不胜感激。
【问题讨论】:
-
Convert与 C# 转换基本相同。同样的规则去。您将不得不使用一些转换方法。 -
啊,那也许是 Expression.Call 然后是一些转换方法?
-
从下面提供的链接中,使用
Convert.ChangeType。您拥有所需的所有信息。 -
我遇到了一些麻烦。我的表情符很弱。
-
Expression.Call(typeof(Convert).GetMethod(...), sourceExpressionProperty, Expression.Quote(typecode))
标签: c# lambda expression-trees