本文整理汇总了C#中SwitchStatementSyntax类的典型用法代码示例。如果您正苦于以下问题:C# SwitchStatementSyntax类的具体用法?C# SwitchStatementSyntax怎么用?C# SwitchStatementSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SwitchStatementSyntax类属于命名空间,在下文中一共展示了SwitchStatementSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BoundSwitchStatement
public BoundSwitchStatement(BoundExpression boundCondition, List<BoundCase> boundCases, BoundElse boundElse, SwitchStatementSyntax syntax)
: base(syntax)
{
BoundCondition = boundCondition;
BoundCases = boundCases;
BoundElse = boundElse;
}
示例2: VisitSwitchStatement
public override SyntaxNode VisitSwitchStatement(SwitchStatementSyntax node)
{
var section = node.Sections.FirstOrDefault();
if (section != null)
{
if (_isExpressionTransformer)
{
_isExpressionTransformer = false;
Visit(section);
if (_transform)
{
_isExpressionTransformer = false;
_transform = false;
return node.AddSections(
Syntax.SwitchSection(
Syntax.List(
Syntax.SwitchLabel(
SyntaxKind.CaseSwitchLabel,
Syntax.ParseExpression(string.Concat("\"", _expressionTransformerNamespaceDot, _edmxName, "ClientContext\"")))),
Syntax.List(
Syntax.ParseStatement(string.Concat("return new ", _expressionTransformerNamespaceDot, _edmxName, @"ExpressionTransformer().TransformExpression(expression, contextName);")))));
}
}
}
return base.VisitSwitchStatement(node);
}
示例3: GetMissingEnumMembers
//--- Class Methods ---
public static IEnumerable<ISymbol> GetMissingEnumMembers(SwitchStatementSyntax switchNode, SemanticModel semanticModel, out IdentifierNameSyntax switchVariable)
{
// TODO (2016-02-25, steveb): what if the swich calls a function instead?
switchVariable = switchNode.Expression as IdentifierNameSyntax;
if(switchVariable == null) {
return Enumerable.Empty<ISymbol>();
}
var switchVariableTypeInfo = semanticModel.GetTypeInfo(switchVariable);
// check if we are switching over an enum
if((switchVariableTypeInfo.Type != null) && (switchVariableTypeInfo.Type.TypeKind == TypeKind.Enum)) {
// get all the enum values
var enumMembers = switchVariableTypeInfo.Type.GetMembers().Where(x => x.Kind == SymbolKind.Field).ToImmutableArray();
// get all case statements
var caseSwitchLabels = switchNode.Sections
.SelectMany(section => section.Labels)
.Select(label => label.DescendantNodes().OfType<MemberAccessExpressionSyntax>().FirstOrDefault())
.Where(memberAccess => memberAccess != null)
.Select(memberAccess => semanticModel.GetSymbolInfo(memberAccess).Symbol)
.ToImmutableHashSet();
// make sure we have all cases covered
return enumMembers.Where(x => !caseSwitchLabels.Contains(x)).ToImmutableArray();
}
return Enumerable.Empty<ISymbol>();
}
示例4: SwitchStatement
public static string SwitchStatement(SwitchStatementSyntax statement)
{
var output = "switch ";
output += SyntaxNode(statement.Expression) + " {" + NewLine;
output = statement.Sections.Aggregate(output, (current, sect) => current + SyntaxNode(sect));
return output + "}" + NewLine;
}
示例5: BindSwitchStatement
private BoundStatement BindSwitchStatement(SwitchStatementSyntax syntax, Symbol parent)
{
BindAttributes(syntax.Attributes);
var switchBinder = new Binder(_sharedBinderState, this);
var boundSections = syntax.Sections.Select(x => switchBinder.Bind(x, y => switchBinder.BindSwitchSection(y, parent))).ToImmutableArray();
return new BoundSwitchStatement(
Bind(syntax.Expression, BindExpression),
boundSections);
}
示例6: HasPatternSwitchSyntax
private static bool HasPatternSwitchSyntax(SwitchStatementSyntax switchSyntax)
{
foreach (var section in switchSyntax.Sections)
{
if (section.Labels.Any(SyntaxKind.CasePatternSwitchLabel))
{
return true;
}
}
return false;
}
示例7: DefaultSectionThrows
private bool DefaultSectionThrows(SwitchStatementSyntax switchStatement, SemanticModel semanticModel)
{
var defaultCase = switchStatement.Sections.FirstOrDefault(s => s.Labels.OfType<DefaultSwitchLabelSyntax>().Any());
if (defaultCase == null)
{
return false;
}
bool throwsInvalidOperation =
defaultCase.Statements
// Filtering throw
.OfType<ThrowStatementSyntax>()
.Select(s => s.Expression)
// Filtering throw new Exception
.OfType<ObjectCreationExpressionSyntax>()
// Filtering unknown symbols
.Select(s => semanticModel.GetSymbolInfo(s.Type).Symbol)
.Where(s => s != null)
// True, if throw new InvalidOperationException()
.Any(s => s.Equals(semanticModel.GetClrType(typeof(InvalidOperationException))));
if (throwsInvalidOperation)
{
return true;
}
bool hasContractAssertOrAssumeWithFalse =
defaultCase.Statements
// Getting only expressions
.OfType<ExpressionStatementSyntax>()
// that calls functions
.Select(s => s.Expression as InvocationExpressionSyntax)
.Where(s => s != null)
// with first argument equals to false
.Where(s =>
s.ArgumentList.Arguments.FirstOrDefault()?.Expression?.Kind() == SyntaxKind.FalseLiteralExpression)
// with known symbols
.Select(s => semanticModel.GetSymbolInfo(s).Symbol as IMethodSymbol)
.Where(s => s != null)
// Contract.Assert/Assume or Debug.Assert
.Any(m => (m.ContainingType.Equals(semanticModel.GetClrType(typeof(Contract))) &&
(m.Name == "Assert" || m.Name == "Assume")) ||
(m.ContainingType.Name == "Debug" && m.Name == "Assert"));
if (hasContractAssertOrAssumeWithFalse)
{
return true;
}
return false;
}
示例8: Create
internal static SwitchBinder Create(Binder next, SwitchStatementSyntax switchSyntax)
{
var parseOptions = switchSyntax?.SyntaxTree?.Options as CSharpParseOptions;
return
// In C# 6 and earlier, we use the old binder. In C# 7 and later, we use the new binder which
// is capable of binding both the old and new syntax. However, the new binder does not yet
// lead to a translation that fully supports edit-and-continue, so it delegates to the C# 6
// binder when it can. The "typeswitch" feature flag forces the use of the C# 7 switch binder
// for all operations; we use it to enhance test coverage.
(parseOptions?.IsFeatureEnabled(MessageID.IDS_FeaturePatternMatching) != false || parseOptions?.Features.ContainsKey("typeswitch") != false)
? new PatternSwitchBinder(next, switchSyntax)
: new SwitchBinder(next, switchSyntax);
}
示例9: ExplodeSwitch
private async Task<Document> ExplodeSwitch(Document document,
SyntaxNode root,
SemanticModel semanticModel,
SwitchStatementSyntax node,
CancellationToken cancellationToken)
{
var switchRewriter = new SwitchSyntaxRewriter(semanticModel);
var newNode = switchRewriter.VisitSwitchStatement(node);
root = root.ReplaceNode(node, newNode);
document = document.WithSyntaxRoot(root);
return await Formatter.FormatAsync(document, null, cancellationToken).ConfigureAwait(false);
}
示例10: SwitchAnalyzer
public SwitchAnalyzer(SwitchStatementSyntax switchStatement, SemanticModel semanticModel)
{
// Hm...! It seems that Code Contracts still has a bug, and uncommenting Contract.Requires lead to NRE!
//Contract.Requires(switchStatement != null);
//Contract.Requires(semanticModel != null);
_switchStatement = switchStatement;
_semanticModel = semanticModel;
var expressionSymbol = semanticModel.GetSymbolInfo(switchStatement.Expression).Symbol;
_expressionType = LazyEx.Create(() => GetSymbolType(expressionSymbol));
_switchOverEnum = LazyEx.Create(() => _expressionType.Value.IsEnum());
_enumValues = LazyEx.Create(() => _expressionType.Value.GetSortedEnumFieldsAndValues().ToImmutableList());
_cases = LazyEx.Create(() => GetUsedCases().ToImmutableList());
}
示例11: BindSwitchExpressionAndSections
internal override BoundStatement BindSwitchExpressionAndSections(SwitchStatementSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
{
// If it is a valid C# 6 switch statement, we use the old binder to bind it.
if (!UseV7SwitchBinder) return base.BindSwitchExpressionAndSections(node, originalBinder, diagnostics);
Debug.Assert(SwitchSyntax.Equals(node));
// Bind switch expression and set the switch governing type.
var boundSwitchExpression = SwitchGoverningExpression;
diagnostics.AddRange(SwitchGoverningDiagnostics);
BoundPatternSwitchLabel defaultLabel;
ImmutableArray<BoundPatternSwitchSection> switchSections = BindPatternSwitchSections(boundSwitchExpression, node.Sections, originalBinder, out defaultLabel, diagnostics);
var locals = GetDeclaredLocalsForScope(node);
var functions = GetDeclaredLocalFunctionsForScope(node);
return new BoundPatternSwitchStatement(
node, boundSwitchExpression,
locals, functions, switchSections, defaultLabel, this.BreakLabel, this);
}
示例12: GetCaseLabels
public static List<ExpressionSyntax> GetCaseLabels(SwitchStatementSyntax switchStatement, out bool containsDefaultLabel)
{
containsDefaultLabel = false;
var caseLabels = new List<ExpressionSyntax>();
foreach (var section in switchStatement.Sections)
{
foreach (var label in section.Labels)
{
var caseLabel = label as CaseSwitchLabelSyntax;
if (caseLabel != null)
{
caseLabels.Add(caseLabel.Value);
}
if (label.IsKind(SyntaxKind.DefaultSwitchLabel))
{
containsDefaultLabel = true;
}
}
}
return caseLabels;
}
示例13: BindSwitchExpressionAndSections
internal virtual BoundSwitchStatement BindSwitchExpressionAndSections(SwitchStatementSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
{
return this.Next.BindSwitchExpressionAndSections(node, originalBinder, diagnostics);
}
示例14: HasAtLeastThreeLabels
private static bool HasAtLeastThreeLabels(SwitchStatementSyntax node)
{
return node.Sections.Sum(section => section.Labels.Count) >= 3;
}
示例15: BindSwitchExpressionAndSections
internal override BoundStatement BindSwitchExpressionAndSections(SwitchStatementSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
{
Debug.Assert(SwitchSyntax.Equals(node));
// Bind switch expression and set the switch governing type.
var boundSwitchExpression = this.SwitchGoverningExpression;
diagnostics.AddRange(this.SwitchGoverningDiagnostics);
// Switch expression might be a constant expression.
// For this scenario we can determine the target label of the switch statement
// at compile time.
LabelSymbol constantTargetOpt = null;
var constantValue = boundSwitchExpression.ConstantValue;
if (constantValue != null)
{
constantTargetOpt = BindConstantJumpTarget(constantValue, node);
}
else if (!node.Sections.Any())
{
// empty switch block, set the break label as target
constantTargetOpt = this.BreakLabel;
}
// Bind switch section
ImmutableArray<BoundSwitchSection> boundSwitchSections = BindSwitchSections(node.Sections, originalBinder, diagnostics);
return new BoundSwitchStatement(node, null, boundSwitchExpression, constantTargetOpt,
GetDeclaredLocalsForScope(node),
GetDeclaredLocalFunctionsForScope(node), boundSwitchSections, this.BreakLabel, null);
}