Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime generated expression cannot change dictionary's values

I'm trying to create some expression at runtime to change a given dictionary's values. I created this snippet which generates the expression successfully and compiles it an Action. But calling the action cannot modify dictionary's value, and also doesn't throw any error. Here is the code:

public class ChangeDicValue {

    public void Change(IDictionary<string, object> dic) {
        var blocks = MakeCleaningBlock(dic);
        foreach (var block in blocks) 
            block.Invoke(dic);
    }

    private List<Action<IDictionary<string, Object>>> MakeCleaningBlock(IDictionary<string , object > dic) {

        var allKeys = dic.Keys.ToArray();

        var dicType = typeof(IDictionary<,>).MakeGenericType(typeof(string), typeof(object));

        var dicContainsMethod = dicType.GetMethod("ContainsKey", new[] {typeof(string)})
                                ?? throw new InvalidOperationException();

        var actions = new List<Action<IDictionary<string, Object>>>();

        ParameterExpression actionArguments =
            Expression.Parameter(dicType, "actionArguments");

        foreach (var k in allKeys) {

            Expression key = Expression.Constant(k, typeof(string));

            Expression target = Expression.Property(actionArguments, "Item", key);

            var innerStatements = new List<Expression>(Changers);

            var cleanStatements = new List<Expression>();

            foreach (var ins in innerStatements) {
                var assign = Expression.Assign(target, Expression.Block(ins, target));

                cleanStatements.Add(assign);
            }

            Expression body1 = Expression.Block(new List<Expression>(cleanStatements) {target});

            var callToContains = Expression.Call(actionArguments, dicContainsMethod, key);
            var ifThenBody     = Expression.IfThen(callToContains, body1);

            var cleanedValueBlock = Expression.Block(target, ifThenBody, target);

            var assignDic = Expression.Assign(target, cleanedValueBlock);
            // see the debug view of assignDic in UPDATE

            var lambda = Expression.Lambda<Action<IDictionary<string, Object>>>(assignDic, actionArguments);

            var method = lambda.Compile();

            actions.Add(method);
        }

        return actions;
    }


    private static readonly Expression<Func<object, string>>[] Changers
        = {
            s => s + " First changer added.", 
            s => s + " Second changer added."
        };

}

As you can see, it's a pretty simple code and causen't any error. Do you have any idea what I missed?

EDIT:

The debug view of variable assignDic for one item in a sample dictionary:

$actionArguments.Item["a"] = .Block() {
    $actionArguments.Item["a"];
    .If (
        .Call $actionArguments.ContainsKey("a")
    ) {
        .Block() {
            $actionArguments.Item["a"] = .Block() {
                .Lambda #Lambda1<System.Func`2[System.Object,System.String]>;
                $actionArguments.Item["a"]
            };
            $actionArguments.Item["a"] = .Block() {
                .Lambda #Lambda2<System.Func`2[System.Object,System.String]>;
                $actionArguments.Item["a"]
            };
            $actionArguments.Item["a"]
        }
    } .Else {
        .Default(System.Void)
    };
    $actionArguments.Item["a"]
}

.Lambda #Lambda1<System.Func`2[System.Object,System.String]>(System.Object $s) {
    $s + " First changer added."
}

.Lambda #Lambda2<System.Func`2[System.Object,System.String]>(System.Object $s) {
    $s + " Second changer added."
}
like image 476
amiry jd Avatar asked Jan 18 '20 13:01

amiry jd


1 Answers

OK. Finally I found the problem & solution. The break point of the code was on the assignment in the inner foreach loop, where I was trying to assign an Expression.Block to a IndexerExpression. It seems blocking an expression won't call it. So, I changed it to an InvokationExpression by calling Expression.Invoke and passing the IndexerExpression (named target) and now it works like a charm:

foreach (var ins in innerStatements) {
    var assign = Expression.Assign(target, Expression.Invoke(ins, target));
    cleanStatements.Add(assign);
}
like image 116
amiry jd Avatar answered Sep 30 '22 00:09

amiry jd