本文整理汇总了C#中SwitchStatement类的典型用法代码示例。如果您正苦于以下问题:C# SwitchStatement类的具体用法?C# SwitchStatement怎么用?C# SwitchStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SwitchStatement类属于命名空间,在下文中一共展示了SwitchStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitSwitchStatement
public override object VisitSwitchStatement(SwitchStatement switchStatement, object data)
{
foreach (var section in switchStatement.SwitchSections)
{
foreach (var label in section.CaseLabels)
{
var memberRef = label.Expression as MemberReferenceExpression;
var targetObject = memberRef != null ? memberRef.Target as IdentifierExpression : null;
if (targetObject != null)
{
var hasEnums = codebaseDeclarationInfos.Any(a =>
a.Name == targetObject.Identifier &&
a.DeclarationClassType == TypeDeclarationKind.Enum);
if (hasEnums)
{
UnlockWith(switchStatement);
break;
}
}
}
}
return base.VisitSwitchStatement(switchStatement, data);
}
示例2: VisitSwitchStatement
public override void VisitSwitchStatement(SwitchStatement switchStatement)
{
var token = CreateBlock(string.Format("switch ({0})", switchStatement.Expression.GetText()), SDNodeRole.Switch);
_tokenList.Add(token);
var tmp = _tokenList;
_tokenList = token.Statements;
foreach (var section in switchStatement.SwitchSections)
{
var caseToken = CreateBlock(string.Join(" ", section.CaseLabels.Select(o => o.GetText())), SDNodeRole.Case);
_tokenList.Add(caseToken);
VisitChildren(caseToken.Statements, section);
}
}
示例3: Visit
public override object Visit (Switch switchStatement)
{
var result = new SwitchStatement ();
var location = LocationsBag.GetLocations (switchStatement);
result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), "switch".Length), SwitchStatement.Roles.Keyword);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), SwitchStatement.Roles.LPar);
result.AddChild ((INode)switchStatement.Expr.Accept (this), SwitchStatement.Roles.Expression);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), SwitchStatement.Roles.RPar);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[2]), 1), SwitchStatement.Roles.LBrace);
foreach (var section in switchStatement.Sections) {
var newSection = new MonoDevelop.CSharp.Dom.SwitchSection ();
foreach (var caseLabel in section.Labels) {
var newLabel = new CaseLabel ();
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), "case".Length), SwitchStatement.Roles.Keyword);
if (caseLabel.Label != null)
newLabel.AddChild ((INode)caseLabel.Label.Accept (this), SwitchStatement.Roles.Expression);
newSection.AddChild (newLabel, MonoDevelop.CSharp.Dom.SwitchSection.CaseLabelRole);
}
newSection.AddChild ((INode)section.Block.Accept (this), MonoDevelop.CSharp.Dom.SwitchSection.Roles.Body);
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
}
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[3]), 1), SwitchStatement.Roles.RBrace);
return result;
}
示例4: Visit
public override object Visit(Switch switchStatement)
{
var result = new SwitchStatement();
var location = LocationsBag.GetLocations(switchStatement);
result.AddChild(new CSharpTokenNode(Convert(switchStatement.loc), SwitchStatement.SwitchKeywordRole), SwitchStatement.SwitchKeywordRole);
if (location != null)
result.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.LPar), Roles.LPar);
if (switchStatement.Expr != null)
result.AddChild((Expression)switchStatement.Expr.Accept(this), Roles.Expression);
if (location != null && location.Count > 1)
result.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.RPar), Roles.RPar);
if (location != null && location.Count > 2)
result.AddChild(new CSharpTokenNode(Convert(location [2]), Roles.LBrace), Roles.LBrace);
SwitchSection newSection = null;
bool lastWasCase = false, added = true;
if (switchStatement.Block != null) {
foreach (var child in switchStatement.Block.Statements) {
var statement = child.Accept(this);
var caseLabel = statement as CaseLabel;
if (caseLabel != null) {
if (!lastWasCase) {
newSection = new SwitchSection();
added = false;
}
newSection.AddChild(caseLabel, SwitchSection.CaseLabelRole);
lastWasCase = true;
} else {
if (lastWasCase) {
result.AddChild(newSection, SwitchStatement.SwitchSectionRole);
lastWasCase = false;
added = true;
}
newSection.AddChild((Statement)statement, Roles.EmbeddedStatement);
}
}
}
if (!added)
result.AddChild(newSection, SwitchStatement.SwitchSectionRole);
if (location != null && location.Count > 3) {
result.AddChild(new CSharpTokenNode(Convert(location [3]), Roles.RBrace), Roles.RBrace);
} else {
// parser error, set end node to max value.
result.AddChild(new ErrorNode(), Roles.Error);
}
return result;
}
示例5: Visit
public override object Visit(Switch switchStatement)
{
var result = new SwitchStatement ();
var location = LocationsBag.GetLocations (switchStatement);
result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), SwitchStatement.SwitchKeywordRole), SwitchStatement.SwitchKeywordRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
if (switchStatement.Expr != null)
result.AddChild ((Expression)switchStatement.Expr.Accept (this), Roles.Expression);
if (location != null && location.Count > 1)
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
if (location != null && location.Count > 2)
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace);
if (switchStatement.Sections != null) {
foreach (var section in switchStatement.Sections) {
var newSection = new SwitchSection ();
if (section.Labels != null) {
foreach (var caseLabel in section.Labels) {
var newLabel = new CaseLabel ();
if (caseLabel.Label != null) {
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), CaseLabel.CaseKeywordRole), CaseLabel.CaseKeywordRole);
if (caseLabel.Label != null)
newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), Roles.Expression);
var colonLocation = LocationsBag.GetLocations (caseLabel);
if (colonLocation != null)
newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), Roles.Colon), Roles.Colon);
} else {
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), CaseLabel.DefaultKeywordRole), CaseLabel.DefaultKeywordRole);
newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length), Roles.Colon), Roles.Colon);
}
newSection.AddChild (newLabel, SwitchSection.CaseLabelRole);
}
}
var blockStatement = section.Block;
var bodyBlock = new BlockStatement ();
int curLocal = 0;
AddBlockChildren (bodyBlock, blockStatement, ref curLocal);
foreach (var statement in bodyBlock.Statements) {
statement.Remove ();
newSection.AddChild (statement, Roles.EmbeddedStatement);
}
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
}
}
if (location != null && location.Count > 3) {
result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace);
} else {
// parser error, set end node to max value.
result.AddChild (new ErrorNode (), Roles.Error);
}
return result;
}
示例6: PostWalk
protected internal virtual void PostWalk(SwitchStatement node) { }
示例7: LocalRef
/*private LocalRef CreateLoopVariable(string name)
{
LocalRef ret = new LocalRef(name);
GetScope().AddSymbol(ret);
return ret;
}
private void RemoveLoopVariable(LocalRef var)
{
GetScope().RemoveSymbol(var);
}*/
private void AddCase(SwitchStatement ss, object label, Statement block)
{
ss.cases.Add(new SwitchStatement.Case(label, block));
}
示例8: Visit
public virtual void Visit(SwitchStatement.CaseStatement s)
{
VisitChildren(s);
if (s.ArgumentList != null)
s.ArgumentList.Accept(this);
if (s.LastExpression != null)
s.LastExpression.Accept(this);
}
示例9: SwitchStatement
public void SwitchStatement()
{
SwitchStatement type = new SwitchStatement {
Expression = new IdentifierExpression("i"),
SwitchSections = {
new SwitchSection {
CaseLabels = {
new CaseLabel(new PrimitiveExpression(1)),
new CaseLabel(new PrimitiveExpression(2))
},
Statements = {
new ExpressionStatement(new IdentifierExpression("A").Invoke()),
new ExpressionStatement(new IdentifierExpression("B").Invoke()),
new BreakStatement()
}
},
new SwitchSection {
CaseLabels = {
new CaseLabel(new PrimitiveExpression(3))
},
Statements = {
new BlockStatement {
new VariableDeclarationStatement(new PrimitiveType("int"), "a", new PrimitiveExpression(3)),
new ReturnStatement(new IdentifierExpression("a")),
}
}
},
new SwitchSection {
CaseLabels = {
new CaseLabel()
},
Statements = {
new BreakStatement()
}
}
}};
AssertOutput("switch (i) {\ncase 1:\ncase 2:\n$A ();\n$B ();\n$break;\n" +
"case 3: {\n$int a = 3;\n$return a;\n}\n" +
"default:\n$break;\n}\n", type);
}
示例10: SwitchWithGotoCase
public void SwitchWithGotoCase()
{
SwitchStatement @switch = new SwitchStatement {
Expression = new PrimitiveExpression(1),
SwitchSections = {
new SwitchSection { // case 0:
CaseLabels = { new CaseLabel(new PrimitiveExpression(0)) },
Statements = { new BreakStatement() }
},
new SwitchSection { // case 1:
CaseLabels = { new CaseLabel(new PrimitiveExpression(1)) },
Statements = {
new ExpressionStatement(new AssignmentExpression(new IdentifierExpression("a"), new PrimitiveExpression(0))),
new GotoCaseStatement { LabelExpression = new PrimitiveExpression(2) }
}
},
new SwitchSection { // case 2:
CaseLabels = { new CaseLabel(new PrimitiveExpression(2)) },
Statements = { new BreakStatement() }
}
}};
SwitchSection case0 = @switch.SwitchSections.ElementAt(0);
SwitchSection case1 = @switch.SwitchSections.ElementAt(1);
SwitchSection case2 = @switch.SwitchSections.ElementAt(2);
DefiniteAssignmentAnalysis da = CreateDefiniteAssignmentAnalysis(@switch);
da.Analyze("a");
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(@switch));
Assert.AreEqual(DefiniteAssignmentStatus.CodeUnreachable, da.GetStatusBefore(case0.Statements.First()));
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(case1.Statements.First()));
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusBefore(case2.Statements.First()));
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusAfter(@switch));
}
示例11: VisitSwitchStatement
public virtual void VisitSwitchStatement(SwitchStatement switchStatement)
{
if (this.ThrowException)
{
throw (Exception)this.CreateException(switchStatement);
}
}
示例12: Visit
public void Visit(SwitchStatement expression)
{
outStream.Write("switch (");
expression.Expression.Accept(this);
outStream.WriteLine(") {");
foreach (var cc in expression.CaseClauses)
{
if (cc.IsDefault)
outStream.WriteLine("default:");
else
{
outStream.Write("case ");
cc.Expression.Accept(this);
outStream.WriteLine(":");
}
VisitStatements(cc.Statements);
}
outStream.Write("}");
}
示例13: actionPerformed
/**
* Call back method that must be called as soon as the given <code>
* SwitchStatement</code> object has been traversed.
*
* @param pSwitchStatement The <code>SwitchStatement</code> object that has
* just been traversed.
*/
public void actionPerformed(
SwitchStatement pSwitchStatement)
{
// Nothing to do.
}
示例14: performAction
/**
* Call back method that must be called when the given <code>
* SwitchStatement</code> will become the next <i>traverse candidate</i>.
*
* @param pSwitchStatement The <code>SwitchStatement</code> object that
* will become the next <i>traverse candidate</i>.
*/
public void performAction(
SwitchStatement pSwitchStatement)
{
// Nothing to do.
}
示例15: VisitSwitchStatement
public override void VisitSwitchStatement(SwitchStatement switchStatement)
{
ForceSpacesBefore(switchStatement.LParToken, policy.SpaceBeforeSwitchParentheses);
ForceSpacesAfter(switchStatement.LParToken, policy.SpacesWithinSwitchParentheses);
ForceSpacesBefore(switchStatement.RParToken, policy.SpacesWithinSwitchParentheses);
EnforceBraceStyle(policy.StatementBraceStyle, switchStatement.LBraceToken, switchStatement.RBraceToken);
VisitChildren(switchStatement);
}