wil*_*ill 2 c# lambda expression-trees
我正在尝试转换参数表达式,但无法转换为值类型。以下是我的代码示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下无效的操作异常:
No coercion operator is defined between types 'System.String' and 'System.Decimal'.
此转换的任何帮助将不胜感激。
小智 6
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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,结果表达式是对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);
}
}
Run Code Online (Sandbox Code Playgroud)