简体   繁体   English

“System.Int32”类型的ParameterExpression不能用于“System.String”类型的委托参数

[英]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 . 然而, Ageint类型,而不是string

You can solve this in at least two ways: 您可以通过至少两种方式解决此问题:

  1. Add a second generic parameter to your GetAction method that specifies the return type. GetAction方法添加第二个泛型参数,以指定返回类型。
  2. Add a call to ToString on the returned property to the expression. 在返回的属性上向表达式添加对ToString的调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET FormView:“类型&#39;System.Int32&#39;的对象不能转换为类型&#39;System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 无法在System.String和System.Int32上执行&#39;=&#39;操作? - Cannot perform '=' operation on System.String and System.Int32? C# 无法将“system.string”类型的对象转换为“system.int32”类型 - C# unable to cast object of type 'system.string' to type 'system.int32' 错误:无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error: Unable to cast object of type 'System.Int32' to type 'System.String' 在LINQ查询中无法将类型为&#39;System.Int32&#39;的对象转换为类型为&#39;System.String&#39;的对象 - Unable to cast object of type 'System.Int32' to type 'System.String' in LINQ query Contoso 大学项目:InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型 - Contoso University Project: InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32' 使用 ToListAsync() 时无法将类型为“System.String”的 object 转换为类型“System.Int32” - Unable to cast object of type 'System.String' to type 'System.Int32' while using ToListAsync() 从物化的“System.Int32”类型到“System.String”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid 错误无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error Unable to cast object of type 'System.Int32' to type 'System.String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM