本文整理汇总了C#中IfElseStatement类的典型用法代码示例。如果您正苦于以下问题:C# IfElseStatement类的具体用法?C# IfElseStatement怎么用?C# IfElseStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IfElseStatement类属于命名空间,在下文中一共展示了IfElseStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitIfElseStatement
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
BinaryOperatorExpression boe = ifElseStatement.Condition as BinaryOperatorExpression;
// the BinaryOperatorExpression might be inside a ParenthesizedExpression
if (boe == null && ifElseStatement.Condition is ParenthesizedExpression) {
boe = (ifElseStatement.Condition as ParenthesizedExpression).Expression as BinaryOperatorExpression;
}
if (ifElseStatement.ElseIfSections.Count == 0
&& ifElseStatement.FalseStatement.Count == 0
&& ifElseStatement.TrueStatement.Count == 1
&& boe != null
&& boe.Op == BinaryOperatorType.InEquality
&& (IsNullLiteralExpression(boe.Left) || IsNullLiteralExpression(boe.Right))
)
{
string ident = GetPossibleEventName(boe.Left) ?? GetPossibleEventName(boe.Right);
ExpressionStatement se = ifElseStatement.TrueStatement[0] as ExpressionStatement;
if (se == null) {
BlockStatement block = ifElseStatement.TrueStatement[0] as BlockStatement;
if (block != null && block.Children.Count == 1) {
se = block.Children[0] as ExpressionStatement;
}
}
if (ident != null && se != null) {
InvocationExpression ie = se.Expression as InvocationExpression;
if (ie != null && GetPossibleEventName(ie.TargetObject) == ident) {
ReplaceCurrentNode(new RaiseEventStatement(ident, ie.Arguments));
}
}
}
return base.VisitIfElseStatement(ifElseStatement, data);
}
示例2: VisitIfElseStatement
public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
{
base.VisitIfElseStatement (ifElseStatement);
if (HasRundundantElse(ifElseStatement)) {
AddIssue (ifElseStatement.ElseToken, ctx.TranslateString ("Remove redundant 'else'"),
script =>
{
int start = script.GetCurrentOffset(ifElseStatement.ElseToken.GetPrevNode ().EndLocation);
int end;
var blockStatement = ifElseStatement.FalseStatement as BlockStatement;
if (blockStatement != null) {
if (blockStatement.Statements.Count == 0) {
// remove empty block
end = script.GetCurrentOffset (blockStatement.LBraceToken.StartLocation);
script.Remove (blockStatement);
} else {
// remove block braces
end = script.GetCurrentOffset (blockStatement.LBraceToken.EndLocation);
script.Remove (blockStatement.RBraceToken);
}
} else {
end = script.GetCurrentOffset(ifElseStatement.ElseToken.EndLocation);
}
if (end > start)
script.RemoveText (start, end - start);
script.FormatText (ifElseStatement.Parent);
});
}
}
示例3: VisitIfElseStatement
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
if (!ifElseStatement.FalseStatement.IsNull)
UnlockWith(ifElseStatement);
return base.VisitIfElseStatement(ifElseStatement, data);
}
示例4: VisitIfElseStatement
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
if (ifElseStatement.FalseStatement is IfElseStatement)
UnlockWith(ifElseStatement.ElseToken);
return base.VisitIfElseStatement(ifElseStatement, data);
}
示例5: VisitIfElseStatement
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
var expression = ifElseStatement.Condition as UnaryOperatorExpression;
if (expression != null && expression.Operator == UnaryOperatorType.Not)
UnlockWith(ifElseStatement);
return base.VisitIfElseStatement(ifElseStatement, data);
}
示例6: VisitIfElseStatement
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
var condition = ifElseStatement.Condition as BinaryOperatorExpression;
if (condition != null && condition.Operator != BinaryOperatorType.Equality)
UnlockWith(ifElseStatement);
return base.VisitIfElseStatement(ifElseStatement, data);
}
示例7: VisitIfElseStatement
public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
{
var token = CreateConditionalBlock(ifElseStatement.Condition.GetText());
_tokenList.Add(token);
VisitChildren(token.FalseStatements, ifElseStatement.FalseStatement);
VisitChildren(token.TrueStatements, ifElseStatement.TrueStatement);
}
示例8: HasRundundantElse
bool HasRundundantElse(IfElseStatement ifElseStatement)
{
if (ifElseStatement.FalseStatement.IsNull || ifElseStatement.Parent is IfElseStatement)
return false;
var blockStatement = ifElseStatement.FalseStatement as BlockStatement;
if (blockStatement != null && blockStatement.Statements.Count == 0)
return true;
var reachability = ctx.CreateReachabilityAnalysis (ifElseStatement.TrueStatement);
return !reachability.IsEndpointReachable (ifElseStatement.TrueStatement);
}
示例9: InlineCommentAtEndOfCondition
public void InlineCommentAtEndOfCondition()
{
IfElseStatement condition = new IfElseStatement();
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 1), 2), IfElseStatement.IfKeywordRole);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 4), 1), IfElseStatement.Roles.LPar);
condition.AddChild(new IdentifierExpression("cond", new TextLocation(1, 5)), IfElseStatement.ConditionRole);
condition.AddChild(new Comment(CommentType.MultiLine, new TextLocation(1, 9), new TextLocation(1, 14)) { Content = "a" }, IfElseStatement.Roles.Comment);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 14), 1), IfElseStatement.Roles.RPar);
condition.AddChild(new ReturnStatement(), IfElseStatement.TrueRole);
AssertOutput("if (cond/*a*/)\n$return;\n", condition);
}
示例10: GenerateNewScript
void GenerateNewScript(Script script, IfElseStatement ifStatement)
{
var mergedIfStatement = new IfElseStatement {
Condition = CSharpUtil.InvertCondition(ifStatement.Condition),
TrueStatement = new ContinueStatement()
};
mergedIfStatement.Condition.AcceptVisitor(_insertParenthesesVisitor);
script.Replace(ifStatement, mergedIfStatement);
SimplifyIfFlowAction.InsertBody(script, ifStatement);
}
示例11: CreateAndSplit
static CodeAction CreateAndSplit(RefactoringContext context, IfElseStatement ifStatement, BinaryOperatorExpression bOp)
{
return new CodeAction(
context.TranslateString("Split if"),
script => {
var nestedIf = (IfElseStatement)ifStatement.Clone();
nestedIf.Condition = GetRightSide(bOp);
script.Replace(ifStatement.Condition, GetLeftSide(bOp));
script.Replace(ifStatement.TrueStatement, new BlockStatement { nestedIf });
},
bOp.OperatorToken
);
}
示例12: InsertBody
internal static void InsertBody(Script script, IfElseStatement ifStatement)
{
var ifBody = ifStatement.TrueStatement.Clone();
if (ifBody is BlockStatement) {
AstNode last = ifStatement;
foreach (var stmt in ((BlockStatement)ifBody).Children) {
if (stmt.Role == Roles.LBrace || stmt.Role == Roles.RBrace || stmt.Role == Roles.NewLine)
continue;
script.InsertAfter(last, stmt);
last = stmt;
}
} else {
script.InsertAfter(ifStatement, ifBody);
}
script.FormatText(ifStatement.Parent);
}
示例13: CreateOrSplit
static CodeAction CreateOrSplit(RefactoringContext context, IfElseStatement ifStatement, BinaryOperatorExpression bOp)
{
return new CodeAction(
context.TranslateString("Split if"),
script => {
var newElse = (IfElseStatement)ifStatement.Clone();
newElse.Condition = GetRightSide(bOp);
var newIf = (IfElseStatement)ifStatement.Clone();
newIf.Condition = GetLeftSide(bOp);
newIf.FalseStatement = newElse;
script.Replace(ifStatement, newIf);
script.FormatText(newIf);
},
bOp.OperatorToken
);
}
示例14: Execute
public override void Execute(EditorRefactoringContext context)
{
CSharpFullParseInformation parseInformation = context.GetParseInformation() as CSharpFullParseInformation;
if (parseInformation != null) {
SyntaxTree st = parseInformation.SyntaxTree;
Identifier identifier = (Identifier) st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier);
if (identifier == null)
return;
ParameterDeclaration parameterDeclaration = identifier.Parent as ParameterDeclaration;
if (parameterDeclaration == null)
return;
AstNode grandparent = identifier.Parent.Parent;
if ((grandparent is MethodDeclaration) || (grandparent is ConstructorDeclaration)) {
// Range check condition
var rangeCheck = new IfElseStatement(
new BinaryOperatorExpression(
new BinaryOperatorExpression(new IdentifierExpression(identifier.Name), BinaryOperatorType.LessThan, new IdentifierExpression("lower")),
BinaryOperatorType.ConditionalOr,
new BinaryOperatorExpression(new IdentifierExpression(identifier.Name), BinaryOperatorType.GreaterThan, new IdentifierExpression("upper"))
),
new ThrowStatement(
new ObjectCreateExpression(
new SimpleType("ArgumentOutOfRangeException"),
new List<Expression>() { new PrimitiveExpression(identifier.Name, '"' + identifier.Name + '"'), new IdentifierExpression(identifier.Name), new BinaryOperatorExpression(new PrimitiveExpression("Value must be between "), BinaryOperatorType.Add, new BinaryOperatorExpression(new IdentifierExpression("lower"), BinaryOperatorType.Add, new BinaryOperatorExpression(new PrimitiveExpression(" and "), BinaryOperatorType.Add, new IdentifierExpression("upper")))) }
)
)
);
// Add range check as first statement in method's/constructor's body
var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None);
using (Script script = refactoringContext.StartScript()) {
if (grandparent is MethodDeclaration) {
var methodDeclaration = (MethodDeclaration) grandparent;
script.AddTo(methodDeclaration.Body, rangeCheck);
} else if (grandparent is ConstructorDeclaration) {
var ctorDeclaration = (ConstructorDeclaration) grandparent;
script.AddTo(ctorDeclaration.Body, rangeCheck);
}
}
}
}
}
示例15: VisitIfElseStatement
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
if (ifElseStatement.TrueStatement is BlockStatement)
{
foreach (var statement in (ifElseStatement.TrueStatement as BlockStatement).Statements)
{
if (statement is IfElseStatement)
UnlockWith(ifElseStatement);
}
}
if (ifElseStatement.FalseStatement is BlockStatement)
{
foreach (var statement in (ifElseStatement.FalseStatement as BlockStatement).Statements)
{
if (statement is IfElseStatement)
UnlockWith(ifElseStatement);
}
}
return base.VisitIfElseStatement(ifElseStatement, data);
}