当前位置: 首页>>代码示例>>C#>>正文


C# IfElseStatement.ReplaceWith方法代码示例

本文整理汇总了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);
                            }
                        }
                    }
                }
            }
        }
开发者ID:evanw,项目名称:minisharp,代码行数:38,代码来源:Mangle.cs

示例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);
            }
        }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:14,代码来源:SimplifyZeroAndConditionalExpressionsVisitor.cs

示例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))
//.........这里部分代码省略.........
开发者ID:DKeeper1523,项目名称:ilspy_yh,代码行数:101,代码来源:YuehanTransform.cs


注:本文中的IfElseStatement.ReplaceWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。