![](/img/trans.png)
[英]Is there a way to discover if a ParameterExpression is captured by a BlockExpression or a LambdaExpression
[英]Replace part of BlockExpression
這是一個BlockExpression
var compareTo = GetCompareToExpression<TProperty>(expression, parameters);
var compareToVariable = compareTo.Key;
var compareToCall = compareTo.Value;
var zero = Expression.Constant(0, typeof (int));
LabelTarget ret = Expression.Label(typeof (int));
var block = Expression.Block(new[] {compareToVariable},
Expression.Assign(compareToVariable, compareToCall),
Expression.IfThen(Expression.NotEqual(compareToVariable, zero),
Expression.Return(ret, compareToVariable)),
Expression.Label(ret, zero));
return block;
及其調試視圖:
.Block(System.Int32 $compareItem1) {
$compareItem1 = .Call ($x.Item1).CompareTo($y.Item1);
.If ($compareItem1 != 0) {
.Return #Label1 { $compareItem1 }
} .Else {
.Default(System.Void)
};
.Label
0
.LabelTarget #Label1:
}
現在,我需要用另一種方法中的一些自定義邏輯替換.Default(System.Void)
。 最簡單的方法是什么?
Expression
s是不變的,就像string
一樣。 要對其進行修改,請創建具有所需更改的副本。 通常,您可以使用ExpressionVisitor的子類來執行此操作,例如:
public class DefaultVoidExpressionReplacer : ExpressionVisitor
{
public Expression To;
protected override Expression VisitDefault(DefaultExpression node)
{
if (node.Type == typeof(void))
{
return this.Visit(To);
}
else
{
return base.VisitDefault(node);
}
}
}
您可以像這樣使用它:
var newExpression = new DefaultVoidExpressionReplacer
{ To = replaceExpression }.Visit(yourExpression);
您甚至可以決定在“更高”級別上工作: Expression.IfThen
級別:
protected override Expression VisitConditional(ConditionalExpression node)
{
DefaultExpression de = node.IfFalse as DefaultExpression;
if (de != null && de.Type == typeof(void))
{
return base.Visit(Expression.IfThenElse(node.Test, node.IfTrue, To));
}
return base.VisitConditional(node);
}
由於BlockExpression
不允許您對其進行突變(其Expressions
屬性的類型為ReadOnlyCollection<Expression>
,防止可能的修改),因此您需要從舊塊構建一個新塊。
表達式訪問者提供了一種簡單的編碼方式:
class DefaultReplacer : ExpressionVisitor {
protected override Expression VisitGoto(GotoExpression g) {
if (g.Kind != GotoExpressionKind.Return || g.Value == null) {
return base.VisitGoto(g);
}
// If we are here, it's a return expression with Value.
// Check if Value represents default(System.Void),
// and return a replacement expression here
return ...
}
}
如下使用此訪問者:
Expression modifiedBlock = block.Visit(new DefaultReplacer());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.