本文整理汇总了C#中BinaryOperatorExpression.ReplaceWith方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryOperatorExpression.ReplaceWith方法的具体用法?C# BinaryOperatorExpression.ReplaceWith怎么用?C# BinaryOperatorExpression.ReplaceWith使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryOperatorExpression
的用法示例。
在下文中一共展示了BinaryOperatorExpression.ReplaceWith方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression(binaryOperatorExpression);
var left = binaryOperatorExpression.Left;
var right = binaryOperatorExpression.Right;
if (left is IdentifierExpression &&
right is IdentifierExpression)
{
var leftIdent = (left as IdentifierExpression).Identifier;
var rightIdent = (right as IdentifierExpression).Identifier;
if (leftIdent == rightIdent)
{
if (binaryOperatorExpression.Operator == BinaryOperatorType.Subtract)
{
binaryOperatorExpression.ReplaceWith(new PrimitiveExpression(0));
return;
}
}
}
if (right is PrimitiveExpression &&
(right as PrimitiveExpression).Value is int &&
(int)(right as PrimitiveExpression).Value == 0)
{
if (binaryOperatorExpression.Operator == BinaryOperatorType.Add ||
binaryOperatorExpression.Operator == BinaryOperatorType.Subtract)
{
binaryOperatorExpression.ReplaceWith(left);
return;
}
}
if (binaryOperatorExpression.Operator == BinaryOperatorType.ConditionalAnd)
{
if (ReplaceConditionalAnd(binaryOperatorExpression, left, right) ||
ReplaceConditionalAnd(binaryOperatorExpression, right, left))
return;
}
if (binaryOperatorExpression.Operator == BinaryOperatorType.ConditionalOr)
{
if (ReplaceConditionalOr(binaryOperatorExpression, left, right) ||
ReplaceConditionalOr(binaryOperatorExpression, right, left))
return;
}
}
示例2: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression(binaryOperatorExpression);
// Looking for patterns like:
//
// 2 - 2
var valueLeft = AstHelpers.GetValueFromExpression(binaryOperatorExpression.Left);
var valueRight = AstHelpers.GetValueFromExpression(binaryOperatorExpression.Right);
if (valueLeft == null || valueRight == null)
return;
try
{
dynamic valueResult;
switch (binaryOperatorExpression.Operator)
{
case BinaryOperatorType.Add:
valueResult = valueLeft + valueRight;
break;
case BinaryOperatorType.Subtract:
valueResult = valueLeft - valueRight;
break;
case BinaryOperatorType.Divide:
valueResult = valueLeft / valueRight;
break;
case BinaryOperatorType.Multiply:
valueResult = valueLeft * valueRight;
break;
default:
return;
}
binaryOperatorExpression.ReplaceWith(new PrimitiveExpression(valueResult));
}
catch (Exception)
{
// Can't do anything with this value perhaps, so just ignore it and leave
// the code as-is.
}
}
示例3: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression(binaryOperatorExpression);
// Looking for patterns like:
//
// (x - 2) - 2
// (x / 2) / 2
// (x - 2) + 4
// (x * 2) / 3
var pattern = new BinaryOperatorExpression
{
Left = new ParenthesizedExpression
{
Expression = new BinaryOperatorExpression
{
Left = new AnyNode("left"),
Operator = BinaryOperatorType.Any,
Right = new AnyNode("rightA")
}
},
Operator = BinaryOperatorType.Any,
Right = new AnyNode("rightB")
};
if (binaryOperatorExpression.Operator != BinaryOperatorType.Add &&
binaryOperatorExpression.Operator != BinaryOperatorType.Subtract &&
binaryOperatorExpression.Operator != BinaryOperatorType.Multiply &&
binaryOperatorExpression.Operator != BinaryOperatorType.Divide)
{
return;
}
if (pattern.IsMatch(binaryOperatorExpression))
{
var match = pattern.Match(binaryOperatorExpression);
var outerOperator = binaryOperatorExpression.Operator;
var innerOperator = (BinaryOperatorType)((dynamic)binaryOperatorExpression.Left).Expression.Operator;
var innerValue = AstHelpers.GetValueFromExpression((Expression)match.Get("rightA").First());
var outerValue = AstHelpers.GetValueFromExpression((Expression)match.Get("rightB").First());
if (innerValue == null && outerValue == null)
return;
// If the operators are equal, then we handle expression like (x - 2) - 4.
if (innerOperator == outerOperator)
{
dynamic resultValue;
switch (binaryOperatorExpression.Operator)
{
case BinaryOperatorType.Add:
case BinaryOperatorType.Subtract:
resultValue = innerValue + outerValue;
break;
case BinaryOperatorType.Multiply:
case BinaryOperatorType.Divide:
resultValue = innerValue * outerValue;
break;
default:
return;
}
binaryOperatorExpression.ReplaceWith(new BinaryOperatorExpression(
(match.Get("left").First() as Expression).Clone(),
binaryOperatorExpression.Operator,
new PrimitiveExpression(resultValue)));
return;
}
// Otherwise handle the slightly more complex cases.
PrimitiveExpression result = null;
switch (innerOperator)
{
case BinaryOperatorType.Add:
switch (outerOperator)
{
case BinaryOperatorType.Subtract:
result = new PrimitiveExpression(innerValue - outerValue);
break;
}
break;
case BinaryOperatorType.Subtract:
switch (outerOperator)
{
case BinaryOperatorType.Add:
result = new PrimitiveExpression(innerValue - outerValue);
break;
}
break;
}
if (result != null)
{
binaryOperatorExpression.ReplaceWith(new BinaryOperatorExpression(
(match.Get("left").First() as Expression).Clone(),
binaryOperatorExpression.Operator,
result));
}
}
//.........这里部分代码省略.........
示例4: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression (BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression (binaryOperatorExpression);
var leftT = GetTypeDef (binaryOperatorExpression.Left);
if (leftT != null && !(leftT.IsPrimitive || leftT.FullName=="System.String")) {
var name = "";
switch (binaryOperatorExpression.Operator) {
case BinaryOperatorType.Add:
name = "op_Addition";
break;
case BinaryOperatorType.Subtract:
name = "op_Subtraction";
break;
case BinaryOperatorType.Multiply:
name = "op_Multiply";
break;
case BinaryOperatorType.Divide:
name = "op_Division";
break;
case BinaryOperatorType.Equality:
name = "op_Equality";
break;
case BinaryOperatorType.InEquality:
name = "op_Inequality";
break;
case BinaryOperatorType.LessThan:
name = "op_LessThan";
break;
case BinaryOperatorType.LessThanOrEqual:
name = "op_LessThanOrEqual";
break;
case BinaryOperatorType.GreaterThan:
name = "op_GreaterThan";
break;
case BinaryOperatorType.GreaterThanOrEqual:
name = "op_GreaterThanOrEqual";
break;
}
var m = FindMethod (leftT, name);
if (m != null && m.DeclaringType.FullName != "System.MulticastDelegate") {
var left = binaryOperatorExpression.Left;
var right = binaryOperatorExpression.Right;
left.Remove ();
right.Remove ();
var n = new InvocationExpression (
new MemberReferenceExpression (new IdentifierExpression (leftT.Name), name),
left, right);
n.AddAnnotation (m.ReturnType);
binaryOperatorExpression.ReplaceWith (n);
}
}
}
示例5: VisitBinaryOperatorExpression
public void VisitBinaryOperatorExpression(BinaryOperatorExpression node)
{
VisitChildren(node);
// Force certain operations on integers to stay integers afterwards.
// This allows JavaScript JITs to omit overflow deoptimizations.
if (!WillConvertOperandsToIntegers(node) && !WillConvertOperandsToIntegers(UnparenthesizedParent(node))) {
var result = resolver.Resolve(node) as OperatorResolveResult;
if (result != null && IsIntegerTypeCode(TypeCode(result.Type))) {
var temp = new NullReferenceExpression();
node.ReplaceWith(temp);
temp.ReplaceWith(new BinaryOperatorExpression(node, BinaryOperatorType.BitwiseOr, new PrimitiveExpression(0)));
}
}
}
示例6: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression(binaryOperatorExpression);
// Looking for patterns like:
//
// (x - 2) - x
var pattern = new BinaryOperatorExpression
{
Left = new ParenthesizedExpression
{
Expression = new BinaryOperatorExpression
{
Left = new NamedNode(
"ident",
new IdentifierExpression
{
Identifier = Pattern.AnyString
}),
Operator = BinaryOperatorType.Any,
Right = new AnyNode("other")
}
},
Operator = BinaryOperatorType.Any,
Right = new IdentifierExpressionBackreference("ident")
};
if (pattern.IsMatch(binaryOperatorExpression))
{
var match = pattern.Match(binaryOperatorExpression);
var innerOperator = (BinaryOperatorType)((dynamic)binaryOperatorExpression).Left.Expression.Operator;
var outerOperator = binaryOperatorExpression.Operator;
switch (outerOperator)
{
case BinaryOperatorType.Subtract:
switch (innerOperator)
{
case BinaryOperatorType.Add:
binaryOperatorExpression.ReplaceWith(((AstNode)match.Get("other").First()).Clone());
return;
case BinaryOperatorType.Subtract:
binaryOperatorExpression.ReplaceWith(new UnaryOperatorExpression(
UnaryOperatorType.Minus,
((Expression)match.Get("other").First()).Clone()));
return;
}
break;
}
}
// Looking for patterns like:
//
// (2 - x) + x
pattern = new BinaryOperatorExpression
{
Left = new ParenthesizedExpression
{
Expression = new BinaryOperatorExpression
{
Left = new AnyNode("other"),
Operator = BinaryOperatorType.Any,
Right = new NamedNode(
"ident",
new IdentifierExpression
{
Identifier = Pattern.AnyString
})
}
},
Operator = BinaryOperatorType.Any,
Right = new IdentifierExpressionBackreference("ident")
};
if (pattern.IsMatch(binaryOperatorExpression))
{
var match = pattern.Match(binaryOperatorExpression);
var innerOperator = (BinaryOperatorType)((dynamic) binaryOperatorExpression).Left.Expression.Operator;
var outerOperator = binaryOperatorExpression.Operator;
switch (outerOperator)
{
case BinaryOperatorType.Add:
switch (innerOperator)
{
case BinaryOperatorType.Subtract:
binaryOperatorExpression.ReplaceWith(((AstNode)match.Get("other").First()).Clone());
return;
}
break;
case BinaryOperatorType.Subtract:
switch (innerOperator)
{
case BinaryOperatorType.Add:
binaryOperatorExpression.ReplaceWith(((AstNode)match.Get("other").First()).Clone());
return;
}
break;
}
//.........这里部分代码省略.........