[英]ParameterExpression of type 'System.Int32' cannot be used for delegate parameter of type 'System.String'
I'm creating a general setter using expression tree and here is my code 我正在使用表达式树创建一个通用的setter,这是我的代码
public Expression<Action<T,string>> GetAction<T>(string fieldName)
{
ParameterExpression targetExpr = Expression.Parameter(typeof(T), "Target");
MemberExpression fieldExpr = Expression.Property(targetExpr, fieldName);
ParameterExpression valueExpr = Expression.Parameter(fieldExpr.Type, "value");
UnaryExpression valueCast = (!fieldExpr.Type.IsValueType)
? Expression.TypeAs(valueExpr, fieldExpr.Type)
: Expression.Convert(valueExpr, fieldExpr.Type);
BinaryExpression assignExpr = Expression.Assign(fieldExpr, valueCast);
return Expression.Lambda<Action<T, string>>(assignExpr, targetExpr, valueExpr);
}
I don't call .Compile()
in the above method because I want to examine the expression it builds. 我不在上面的方法中调用
.Compile()
因为我想检查它构建的表达式。
And my object is 我的目标是
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
I call the method like this 我这样称呼方法
var lastname = GetAction<Person>("FirstName");
var age = GetAction<Person>("Age");
lastname.Compile()(p, "Solutions");
age.Compile()(p, "10");
The reason i pass the age as string is, i will be getting this value from XML. 我将age作为字符串传递的原因是,我将从XML中获取此值。
It is creating Action for FirstName
without any error whereas for Age
it blows. 它正在为
FirstName
创建Action而没有任何错误,而对于Age
来说它会被打击。
Error happens in this line for Age
: Age
中此行发生错误:
return Expression.Lambda<Action<T, string>>(assignExpr, targetExpr, valueExpr);
Error: 错误:
ParameterExpression of type 'System.Int32' cannot be used for delegate parameter of type 'System.String'
“System.Int32”类型的ParameterExpression不能用于“System.String”类型的委托参数
Can I do something with dynamic..? 我可以用动态做什么吗?
I'm hoping someone will have some solution. 我希望有人会有一些解决方案。 Thanks
谢谢
You should call Convert.ChangeType
for type conversion: 您应该调用
Convert.ChangeType
进行类型转换:
public static Expression<Action<T, string>> GetAction<T>(string fieldName)
{
ParameterExpression targetExpr = Expression.Parameter(typeof(T), "Target");
MemberExpression fieldExpr = Expression.Property(targetExpr, fieldName);
ParameterExpression valueExpr = Expression.Parameter(typeof(string), "value");
MethodCallExpression convertExpr = Expression.Call(typeof(Convert),
"ChangeType", null, valueExpr, Expression.Constant(fieldExpr.Type));
UnaryExpression valueCast = Expression.Convert(convertExpr, fieldExpr.Type);
BinaryExpression assignExpr = Expression.Assign(fieldExpr, valueCast);
return Expression.Lambda<Action<T, string>>(assignExpr, targetExpr, valueExpr);
}
The problem is the following: 问题如下:
You are returning an Expression<Action<T,string>>
, which basically means that the result will be a string
. 您正在返回
Expression<Action<T,string>>
,这基本上意味着结果将是一个string
。 On the other hand, you are passing in "Age" as the name of the field the action should return. 另一方面,您传入“Age”作为操作应返回的字段的名称。
Age
however is of type int
, not string
. 然而,
Age
是int
类型,而不是string
。
You can solve this in at least two ways: 您可以通过至少两种方式解决此问题:
GetAction
method that specifies the return type. GetAction
方法添加第二个泛型参数,以指定返回类型。 ToString
on the returned property to the expression. ToString
的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.