本文整理汇总了C#中IfElseStatement.ReplaceWith方法的典型用法代码示例。如果您正苦于以下问题:C# IfElseStatement.ReplaceWith方法的具体用法?C# IfElseStatement.ReplaceWith怎么用?C# IfElseStatement.ReplaceWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IfElseStatement
的用法示例。
在下文中一共展示了IfElseStatement.ReplaceWith方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitIfElseStatement
public override void VisitIfElseStatement(IfElseStatement node)
{
VisitChildren(node);
var yes = node.TrueStatement;
var no = node.FalseStatement;
var constant = BoolConstant(node.Condition);
// Special-case constant conditions
if (constant == true) {
node.ReplaceWith(yes); // Inline "if (true)"
} else if (constant == false) {
node.ReplaceWith(no); // Inline "if (false)"
}
// Inline single statements
else {
ReplaceBlockWithSingleStatement(no);
if (no.IsNull) {
ReplaceBlockWithSingleStatement(yes);
}
// Be careful to avoid the dangling-else issue
else {
var block = yes as BlockStatement;
if (block != null) {
var statements = block.Statements;
if (statements.Count == 1) {
var first = statements.FirstOrNullObject();
var ifElse = first as IfElseStatement;
if (ifElse == null || !ifElse.FalseStatement.IsNull) {
yes.ReplaceWith(first);
}
}
}
}
}
}
示例2: VisitIfElseStatement
public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
{
base.VisitIfElseStatement(ifElseStatement);
if (ifElseStatement.Condition is PrimitiveExpression &&
(ifElseStatement.Condition as PrimitiveExpression).Value is bool)
{
var value = (bool)(ifElseStatement.Condition as PrimitiveExpression).Value;
if (value)
ifElseStatement.ReplaceWith(ifElseStatement.TrueStatement);
else
ifElseStatement.ReplaceWith(ifElseStatement.FalseStatement);
}
}
示例3: VisitIfElseStatement
public override void VisitIfElseStatement(IfElseStatement e)
{
base.VisitIfElseStatement(e);
#region 删除永远不会成立的条件表达式
//if (!true)
//{
// ……
//}
//====>删除
var ue = e.Condition as UnaryOperatorExpression;
if (ue != null && ue.Expression is PrimitiveExpression)
{
var pe = ue.Expression as PrimitiveExpression;
if (object.Equals(pe.Value, ue.Operator == UnaryOperatorType.Not))
{
// 条件始终不成立
if (e.FalseStatement.IsNull)
{
// 删除:if (false) {……}、if (!true) {……}
e.Remove();
}
else
{
// 用 else 语句体代替条件语句
e.ReplaceWith(e.FalseStatement.Detach());
}
}
else
{
// 条件始终成立
if (e.TrueStatement.IsNull)
{
// 删除:if (true) { }、if (!false) { }
e.Remove();
}
else
{
e.ReplaceWith(e.TrueStatement.Detach());
}
}
return;
}
#endregion
#region 精简方法代理的使用
//if (ModifierGroup.CS$<>9__CachedAnonymousMethodDelegatee == null)
//{
// ModifierGroup.CS$<>9__CachedAnonymousMethodDelegatee = new Action<IChartModifier, ModifierEventArgsBase>(ModifierGroup.TB);
//}
//this.TB(ModifierGroup.CS$<>9__CachedAnonymousMethodDelegatee, e);
//====>
//this.TB(ModifierGroup.TB, e);
var be = e.Condition as BinaryOperatorExpression;
if (be != null &&
be.Operator == BinaryOperatorType.Equality &&
be.Left is MemberReferenceExpression &&
be.Right is NullReferenceExpression &&
e.FalseStatement.IsNull &&
e.TrueStatement.Children.Count() == 1 &&
e.TrueStatement.FirstChild is ExpressionStatement)
{
var fr = be.Left.Annotation<FieldDefinition>();
if (fr == null) return;
if (!fr.IsCompilerGenerated()) return;
var es = e.TrueStatement.FirstChild as ExpressionStatement;
var assignment = es.Expression as AssignmentExpression;
if (assignment == null) return;
if (assignment.Left.Annotation<FieldDefinition>() != fr) return;
Expression methodEpx = null;
var creation = assignment.Right as ObjectCreateExpression;
if (creation != null)
{
var oc = assignment.Right as ObjectCreateExpression;
if (oc.Arguments.Count != 1) return;
var tr = oc.Type.Annotation<TypeReference>();
if (tr == null) return;
var td = tr.Resolve();
if (td == null || !td.IsDelegate()) return;
methodEpx = oc.Arguments.First();
}
else if (assignment.Right is AnonymousMethodExpression)
{
methodEpx = assignment.Right as AnonymousMethodExpression;
}
else
{
methodEpx = assignment.Right as LambdaExpression;
}
if (methodEpx != null)
{
var parent = e.Parent;
e.Remove();
foreach (var x in parent.Descendants.Where(x => x.Annotation<FieldDefinition>() == fr))
//.........这里部分代码省略.........