jav*_*iry 5 c# expression-trees .net-core c#-8.0 .net-core-3.1
我试图在运行时创建一些表达式来更改给定字典的值。我创建了这个片段,它成功地生成了表达式并将其编译为Action. 但是调用动作不能修改字典的值,也不会抛出任何错误。这是代码:
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."
};
}
Run Code Online (Sandbox Code Playgroud)
如您所见,这是一个非常简单的代码,不会导致任何错误。你知道我错过了什么吗?
编辑:
assignDic示例字典中一项的变量调试视图:
$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."
}
Run Code Online (Sandbox Code Playgroud)
好的。最后我找到了问题和解决方案。代码的断点位于内部foreach循环中的赋值上,我试图将 an 分配Expression.Block给 a IndexerExpression。似乎block一个表达式不会调用它。因此,我InvokationExpression通过调用Expression.Invoke并传递IndexerExpression(named target)将其更改为 an ,现在它就像一个魅力:
foreach (var ins in innerStatements) {
var assign = Expression.Assign(target, Expression.Invoke(ins, target));
cleanStatements.Add(assign);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |