Set field value with Expression tree

孤人 提交于 2019-12-23 17:39:02

问题


I need to create an expression for all fields in a class. So I've adopted this post to my needs:

public static void Sample()
{
    var setters = GetFieldSetterExpressions<Account>();
    var myAccount = new Account();
    setters["AccounHolder"](myAccount, "Giovanni");
    setters["Pin"](myAccount, 123);
}

public static Dictionary<string, Action<T, object>> GetFieldSetterExpressions<T>() where T: class 
{

    var dic = new Dictionary<string,Action<T,object>>();

    var type = typeof(T);
    var fields = type.GetFields();
    foreach (var fieldInfo in fields)
    {
        Type fieldType;

        if (IsNullableType(fieldInfo.FieldType))
            fieldType = Nullable.GetUnderlyingType(fieldInfo.FieldType);
        else
            fieldType = fieldInfo.FieldType;


        ParameterExpression targetExp = Expression.Parameter(type, "target");
        ParameterExpression valueExp = Expression.Parameter(fieldType, "value");

        MemberExpression fieldExp = Expression.Field(targetExp, fieldInfo);
        BinaryExpression assingExp = Expression.Assign(fieldExp, valueExp);

        var setter = Expression.Lambda<Action<T, object>>(assingExp, targetExp, valueExp).Compile();

        dic.Add(fieldInfo.Name, setter);
    }

    return dic;
}

My problem is Expression.Lambda<Action<T,object>>(..) since the field can be for example string, I get an exeption. I've tried to intregrate Expression.Convert in my snippet, but couldn't figure out how to implement it.

How do I need to integrate the conversion part in my code?


回答1:


Expression<Action<T, object>> means that the second parameter is expected to be of type object, while your code is using propertyType. Here is how it should be:

var type = typeof(T);
var fields = type.GetFields();
foreach (var fieldInfo in fields)
{
    var targetExp = Expression.Parameter(type, "target");
    var valueExp = Expression.Parameter(typeof(object), "value");

    var fieldExp = Expression.Field(targetExp, fieldInfo);
    var assignExp = Expression.Assign(fieldExp, Expression.Convert(valueExp, fieldExp.Type));

    var setter = Expression.Lambda<Action<T, object>>(assignExp, targetExp, valueExp).Compile();

}


来源:https://stackoverflow.com/questions/37188900/set-field-value-with-expression-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!