我正在尝试转换一个参数表达式,但在转换为值类型时遇到了问题。下面是我的代码示例:
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'.
对此转换的任何帮助都将不胜感激。
发布于 2014-08-05 04:58:20
我采用的解决方案是:
private static Expression GetConvertedSource(ParameterExpression sourceParameter,
PropertyInfo sourceProperty,
TypeCode typeCode)
{
var sourceExpressionProperty = Expression.Property(sourceParameter,
sourceProperty);
var changeTypeCall = Expression.Call(typeof(Convert).GetMethod("ChangeType",
new[] { typeof(object),
typeof(TypeCode) }),
sourceExpressionProperty,
Expression.Constant(typeCode)
);
Expression convert = Expression.Convert(changeTypeCall,
Type.GetType("System." + typeCode));
var convertExpr = Expression.Condition(Expression.Equal(sourceExpressionProperty,
Expression.Constant(null, sourceProperty.PropertyType)),
Expression.Default(Type.GetType("System." + typeCode)),
convert);
return convertExpr;
}请注意处理空值的Expression.Condition。
发布于 2014-08-04 17:48:31
public class ExpressionUtils
{
public static MethodCallExpression ConvertToType(
ParameterExpression sourceParameter,
PropertyInfo sourceProperty,
TypeCode typeCode)
{
var sourceExpressionProperty = Expression.Property(sourceParameter, sourceProperty);
var changeTypeMethod = typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(TypeCode) });
var callExpressionReturningObject = Expression.Call(changeTypeMethod, sourceExpressionProperty, Expression.Constant(typeCode));
return callExpressionReturningObject;
}
}请注意,结果表达式是对Convert.ChangeType方法的调用,该方法将返回System.Object。
这是一个单元测试:
[TestClass]
public class UnitTest1
{
private class MyClass
{
public string ValueAsString { get; set; }
}
[TestMethod]
public void TestMethod1()
{
var parameter = Expression.Parameter(typeof(MyClass));
var property = typeof(MyClass).GetProperty("ValueAsString");
var lambdaBody = ExpressionUtils.ConvertToType(parameter, property, TypeCode.Decimal);
var lambda = Expression.Lambda<Func<MyClass, object>>(lambdaBody, parameter);
var valueAsDecimal = (decimal) lambda.Compile().Invoke(new MyClass { ValueAsString = "42" });
Assert.AreEqual(42m, valueAsDecimal);
}
}https://stackoverflow.com/questions/24941420
复制相似问题