本文整理汇总了C#中SyntaxGenerator.IfStatement方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxGenerator.IfStatement方法的具体用法?C# SyntaxGenerator.IfStatement怎么用?C# SyntaxGenerator.IfStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxGenerator
的用法示例。
在下文中一共展示了SyntaxGenerator.IfStatement方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TriviaKindCheckHelper
internal static SyntaxNode TriviaKindCheckHelper(SyntaxGenerator generator, IfStatementSyntax ifStatement, SyntaxList<SyntaxNode> ifBlockStatements)
{
var ifOneBlock = ifStatement.Statement as BlockSyntax;
var trailingTriviaDeclaration = ifOneBlock.Statements[0] as LocalDeclarationStatementSyntax;
var trailingTrivia = generator.IdentifierName(trailingTriviaDeclaration.Declaration.Variables[0].Identifier.ValueText);
var arguments = new SyntaxList<SyntaxNode>();
var trailingTriviaKind = generator.InvocationExpression(generator.MemberAccessExpression(trailingTrivia, "Kind"), arguments);
var whitespaceTrivia = generator.MemberAccessExpression(generator.IdentifierName("SyntaxKind"), "WhitespaceTrivia");
var equalsExpression = generator.ValueEqualsExpression(trailingTriviaKind, whitespaceTrivia);
var newIfStatement = generator.IfStatement(equalsExpression, ifBlockStatements);
return newIfStatement;
}
示例2: WhitespaceCheckHelper
internal static SyntaxNode WhitespaceCheckHelper(SyntaxGenerator generator, IfStatementSyntax ifStatement, SyntaxList<SyntaxNode> ifBlockStatements)
{
var ifOneBlock = ifStatement.Parent as BlockSyntax;
var ifTwoBlock = ifStatement.Statement as BlockSyntax;
var trailingTriviaDeclaration = ifOneBlock.Statements[0] as LocalDeclarationStatementSyntax;
var trailingTrivia = generator.IdentifierName(trailingTriviaDeclaration.Declaration.Variables[0].Identifier.ValueText);
var arguments = new SyntaxList<SyntaxNode>();
var trailingTriviaToString = generator.InvocationExpression(generator.MemberAccessExpression(trailingTrivia, "ToString"), arguments);
var rightSide = generator.LiteralExpression(" ");
var equalsExpression = generator.ValueEqualsExpression(trailingTriviaToString, rightSide);
var newIfStatement = generator.IfStatement(equalsExpression, ifBlockStatements);
return newIfStatement;
}
示例3: TriviaCountHelper
internal static SyntaxNode TriviaCountHelper(SyntaxGenerator generator, string name, SyntaxList<StatementSyntax> ifBlockStatements)
{
var variableName = generator.IdentifierName(name);
var memberAccess = generator.MemberAccessExpression(variableName, "TrailingTrivia");
var fullMemberAccess = generator.MemberAccessExpression(memberAccess, "Count");
var one = generator.LiteralExpression(1);
var equalsExpression = generator.ValueEqualsExpression(fullMemberAccess, one);
var newIfStatement = generator.IfStatement(equalsExpression, ifBlockStatements);
return newIfStatement;
}
示例4: TriviaCheckHelper
internal static SyntaxNode TriviaCheckHelper(SyntaxGenerator generator, BlockSyntax methodBlock, SyntaxList<StatementSyntax> ifBlockStatements)
{
var secondStatement = methodBlock.Statements[1] as LocalDeclarationStatementSyntax;
var variableName = generator.IdentifierName(secondStatement.Declaration.Variables[0].Identifier.ValueText);
var conditional = generator.MemberAccessExpression(variableName, "HasTrailingTrivia");
var ifStatement = generator.IfStatement(conditional, ifBlockStatements);
return ifStatement;
}
示例5: TriviaKindCheckHelper
// creates the trivia kind check
protected internal static SyntaxNode TriviaKindCheckHelper(SyntaxGenerator generator, IfStatementSyntax ifStatement, SyntaxList<SyntaxNode> ifBlockStatements)
{
var ifBlock = ifStatement.Statement as BlockSyntax;
string variableName = GetTrailingTriviaName(ifBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
var arguments = new SyntaxList<SyntaxNode>();
SyntaxNode whitespaceTrivia = generator.MemberAccessExpression(generator.IdentifierName("SyntaxKind"), "WhitespaceTrivia");
arguments = arguments.Add(whitespaceTrivia);
SyntaxNode trailingTriviaKind = generator.InvocationExpression(generator.MemberAccessExpression(identifierName, "IsKind"), arguments);
SyntaxNode newIfStatement = generator.IfStatement(trailingTriviaKind, ifBlockStatements);
return newIfStatement;
}
示例6: WhitespaceCheckHelper
// creates the whitespace check
protected internal static SyntaxNode WhitespaceCheckHelper(SyntaxGenerator generator, IfStatementSyntax ifStatement, SyntaxList<SyntaxNode> ifBlockStatements)
{
var ifBlock = ifStatement.Parent as BlockSyntax;
string variableName = GetTrailingTriviaName(ifBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
var arguments = new SyntaxList<SyntaxNode>();
SyntaxNode trailingTriviaToString = generator.InvocationExpression(generator.MemberAccessExpression(identifierName, "ToString"), arguments);
SyntaxNode rightSide = generator.LiteralExpression(" ");
SyntaxNode equalsExpression = generator.ValueEqualsExpression(trailingTriviaToString, rightSide);
SyntaxNode newIfStatement = generator.IfStatement(equalsExpression, ifBlockStatements);
return newIfStatement;
}
示例7: Fix
private static async Task<Document> Fix(CodeFixContext context, SyntaxNode root, SyntaxGenerator generator, SemanticModel semanticModel, CancellationToken cancellationToken)
{
SyntaxNode node = root.FindNode(context.Span);
Diagnostic diagnostic = context.Diagnostics.First();
switch (diagnostic.Properties[OperatorOverloadsHaveNamedAlternatesAnalyzer.DiagnosticKindText])
{
case OperatorOverloadsHaveNamedAlternatesAnalyzer.AddAlternateText:
SyntaxNode methodDeclaration = generator.GetDeclaration(node, DeclarationKind.Operator) ?? generator.GetDeclaration(node, DeclarationKind.ConversionOperator);
var operatorOverloadSymbol = (IMethodSymbol)semanticModel.GetDeclaredSymbol(methodDeclaration, cancellationToken);
INamedTypeSymbol typeSymbol = operatorOverloadSymbol.ContainingType;
// For C# the following `typeDeclarationSyntax` and `typeDeclaration` nodes are identical, but for VB they're different so in
// an effort to keep this as language-agnostic as possible, the heavy-handed approach is used.
SyntaxNode typeDeclarationSyntax = typeSymbol.DeclaringSyntaxReferences.First().GetSyntax(cancellationToken);
SyntaxNode typeDeclaration = generator.GetDeclaration(typeDeclarationSyntax,
typeSymbol.TypeKind == TypeKind.Struct ? DeclarationKind.Struct : DeclarationKind.Class);
SyntaxNode addedMember;
IEnumerable<SyntaxNode> bodyStatements = generator.DefaultMethodBody(semanticModel.Compilation);
if (OperatorOverloadsHaveNamedAlternatesAnalyzer.IsPropertyExpected(operatorOverloadSymbol.Name))
{
// add a property
addedMember = generator.PropertyDeclaration(
name: OperatorOverloadsHaveNamedAlternatesAnalyzer.IsTrueText,
type: generator.TypeExpression(SpecialType.System_Boolean),
accessibility: Accessibility.Public,
modifiers: DeclarationModifiers.ReadOnly,
getAccessorStatements: bodyStatements);
}
else
{
// add a method
ExpectedMethodSignature expectedSignature = GetExpectedMethodSignature(operatorOverloadSymbol, semanticModel.Compilation);
if (expectedSignature.Name == "CompareTo" && operatorOverloadSymbol.ContainingType.TypeKind == TypeKind.Class)
{
var nullCheck = generator.IfStatement(
generator.InvocationExpression(
generator.IdentifierName("ReferenceEquals"),
generator.IdentifierName(expectedSignature.Parameters.First().Item1),
generator.NullLiteralExpression()),
new[]
{
generator.ReturnStatement(generator.LiteralExpression(1))
});
bodyStatements = new[] {nullCheck}.Concat(bodyStatements);
}
addedMember = generator.MethodDeclaration(
name: expectedSignature.Name,
parameters: expectedSignature.Parameters.Select(p => generator.ParameterDeclaration(p.Item1, generator.TypeExpression(p.Item2))),
returnType: generator.TypeExpression(expectedSignature.ReturnType),
accessibility: Accessibility.Public,
modifiers: expectedSignature.IsStatic ? DeclarationModifiers.Static : DeclarationModifiers.None,
statements: bodyStatements);
}
SyntaxNode newTypeDeclaration = generator.AddMembers(typeDeclaration, addedMember);
return context.Document.WithSyntaxRoot(root.ReplaceNode(typeDeclaration, newTypeDeclaration));
case OperatorOverloadsHaveNamedAlternatesAnalyzer.FixVisibilityText:
SyntaxNode badVisibilityNode = generator.GetDeclaration(node, DeclarationKind.Method) ?? generator.GetDeclaration(node, DeclarationKind.Property);
ISymbol badVisibilitySymbol = semanticModel.GetDeclaredSymbol(badVisibilityNode, cancellationToken);
SymbolEditor symbolEditor = SymbolEditor.Create(context.Document);
ISymbol newSymbol = await symbolEditor.EditOneDeclarationAsync(badVisibilitySymbol,
(documentEditor, syntaxNode) => documentEditor.SetAccessibility(badVisibilityNode, Accessibility.Public), cancellationToken).ConfigureAwait(false);
Document newDocument = symbolEditor.GetChangedDocuments().Single();
SyntaxNode newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return context.Document.WithSyntaxRoot(newRoot);
default:
return context.Document;
}
}
示例8: TriviaCheckHelper
// creates the HasTrailingTrivia check
protected internal static SyntaxNode TriviaCheckHelper(SyntaxGenerator generator, BlockSyntax methodBlock, SyntaxList<StatementSyntax> ifBlockStatements)
{
string variableName = GetIfKeywordName(methodBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
SyntaxNode conditional = generator.MemberAccessExpression(identifierName, "HasTrailingTrivia");
SyntaxNode ifStatement = generator.IfStatement(conditional, ifBlockStatements);
return ifStatement;
}
示例9: GetConstructorDeclarationSyntax
private static SyntaxNode GetConstructorDeclarationSyntax(
SyntaxGenerator syntaxGenerator,
SemanticModel semanticModel,
string name)
{
// GENERATED CODE:
//
// public Name(MockBehavior behavior = MockBehavior.Strict)
// : base(behavior)
// {
// if (behavior == MockBehavior.Loose)
// {
// ConfigureLooseBehavior();
// }
// }
var mockBehaviorType = syntaxGenerator
.TypeExpression(
semanticModel
.Compilation
.GetTypeByMetadataName("PCLMock.MockBehavior"));
return syntaxGenerator
.ConstructorDeclaration(
name,
parameters: new[]
{
syntaxGenerator
.ParameterDeclaration(
"behavior",
mockBehaviorType,
initializer: syntaxGenerator.MemberAccessExpression(mockBehaviorType, "Strict"))
},
accessibility: Accessibility.Public,
baseConstructorArguments: new[] { syntaxGenerator.IdentifierName("behavior") },
statements: new[]
{
syntaxGenerator.IfStatement(
syntaxGenerator.ValueEqualsExpression(
syntaxGenerator.IdentifierName("behavior"),
syntaxGenerator.MemberAccessExpression(mockBehaviorType, "Loose")),
new[]
{
syntaxGenerator.InvocationExpression(syntaxGenerator.IdentifierName("ConfigureLooseBehavior"))
})
});
}
示例10: CreateDisposeMethods
private IEnumerable<SyntaxNode> CreateDisposeMethods(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, bool suppressWarningComments)
{
yield return g.AddWarningCommentIf(!suppressWarningComments,
g.MethodDeclaration(
"Dispose",
accessibility: Accessibility.Public,
statements: new SyntaxNode[]
{
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
"Dispose"
),
g.TrueLiteralExpression()
),
g.InvocationExpression(
g.MemberAccessExpression(
g.TypeExpression(compilation.RequireTypeByMetadataName("System.GC")),
"SuppressFinalize"
),
g.ThisExpression()
)
}
))
.PrependLeadingTrivia(g.CreateRegionTrivia("IDisposable"));
yield return g.AddWarningCommentIf(!suppressWarningComments, g.MethodDeclaration(
"Dispose",
parameters: new SyntaxNode[] { g.ParameterDeclaration("disposing", g.TypeExpression(SpecialType.System_Boolean)) },
accessibility: Accessibility.Private,
statements: new SyntaxNode[]
{
g.IfStatement(
g.ValueEqualsExpression(
g.IdentifierName("disposing"),
g.TrueLiteralExpression()
),
new SyntaxNode[]
{
g.TryCatchStatement(
new SyntaxNode[]
{
g.ExpressionStatement(
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CloseProxyMethod]
),
g.FalseLiteralExpression()
)
)
},
new SyntaxNode[]
{
g.CatchClause(new SyntaxNode [0])
}
)
}
)
}
)).AddTrailingTrivia(g.CreateEndRegionTrivia());
}
示例11: CreateEnsureProxyMethod
private SyntaxNode CreateEnsureProxyMethod(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, bool asAsync)
{
/*
private async System.Threading.Tasks.Task EnsureProxyAsync()
{
if (m_cachedProxy != null && (
((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Faulted ||
((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Closed))
{
await CloseProxyAsync().ConfigureAwait(false);
}
if (m_cachedProxy == null)
{
var proxy = m_proxyFactory();
await System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)proxy).BeginOpen, ((System.ServiceModel.ICommunicationObject)proxy).EndOpen, null).ConfigureAwait(false);
m_cachedProxy = proxy;
}
}
*/
return
g.MethodDeclaration(
asAsync ? nameTable[MemberNames.EnsureProxyAsyncMethod] : nameTable[MemberNames.EnsureProxyMethod],
returnType: asAsync ? g.TypeExpression(compilation.RequireType<Task>()) : null,
accessibility: Accessibility.Private,
modifiers: asAsync ? DeclarationModifiers.Async : DeclarationModifiers.None,
statements: new SyntaxNode[]
{
//if (m_cachedProxy != null && (
// ((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Faulted ||
// ((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Closed))
g.IfStatement(
g.LogicalAndExpression(
// m_cachedProxy != null
g.ReferenceNotEqualsExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
),
g.NullLiteralExpression()
),
g.LogicalOrExpression(
// ((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Faulted
g.ValueEqualsExpression(
g.MemberAccessExpression(
g.CastExpression(
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"),
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
)
),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Faulted")
),
// ((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Faulted
g.ValueEqualsExpression(
g.MemberAccessExpression(
g.CastExpression(
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"),
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
)
),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Closed")
)
)
),
new SyntaxNode[]
{
// await CloseProxyAsync(false).ConfigureAwait(false);
// or
// CloseProxy(false);
AwaitExpressionIfAsync(g, asAsync,
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
asAsync ? nameTable[MemberNames.CloseProxyAsyncMethod] : nameTable[MemberNames.CloseProxyMethod]
),
g.FalseLiteralExpression()
)
)
}
),
g.IfStatement(
g.ReferenceEqualsExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
),
g.NullLiteralExpression()
),
new SyntaxNode[]
{
g.LocalDeclarationStatement(
"proxy",
//.........这里部分代码省略.........
示例12: CreateCloseProxyMethod
private SyntaxNode CreateCloseProxyMethod(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, bool asAsync)
{
return
g.MethodDeclaration(
asAsync ? nameTable[MemberNames.CloseProxyAsyncMethod] : nameTable[MemberNames.CloseProxyMethod],
returnType: asAsync ? g.TypeExpression(compilation.RequireType<Task>()) : null,
parameters: new SyntaxNode[] { g.ParameterDeclaration("alwaysAbort", g.TypeExpression(SpecialType.System_Boolean)) },
modifiers: asAsync ? DeclarationModifiers.Async : DeclarationModifiers.None,
accessibility: Accessibility.Private,
statements: new SyntaxNode[]
{
g.IfStatement(
g.ReferenceNotEqualsExpression(
g.IdentifierName(nameTable[MemberNames.CachedProxyField]),
g.NullLiteralExpression()
),
new SyntaxNode[]
{
g.LocalDeclarationStatement(
"proxy",
g.TryCastExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
),
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject")
)
),
g.TryFinallyStatement(
new SyntaxNode[]
{
AwaitExpressionIfAsync(g, asAsync,
g.InvocationExpression(
g.IdentifierName(asAsync ? nameTable[MemberNames.CloseProxyAsyncMethod] : nameTable[MemberNames.CloseProxyMethod]),
g.IdentifierName("proxy"),
g.IdentifierName("alwaysAbort")
)
)
},
new SyntaxNode[]
{
g.AssignmentStatement(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
),
g.NullLiteralExpression()
)
}
)
}
)
}
);
}
示例13: CreateStaticCloseProxyMethod
private SyntaxNode CreateStaticCloseProxyMethod(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, bool asAsync)
{
//private static void CloseProxy(System.ServiceModel.ICommunicationObject proxy, bool alwaysAbort)
//{
// try
// {
// if (proxy != null && proxy.State != System.ServiceModel.CommunicationState.Closed)
// {
// if (!alwaysAbort && proxy.State != System.ServiceModel.CommunicationState.Faulted)
// {
// proxy.Close();
// }
// else
// {
// proxy.Abort();
// }
// }
// }
// catch (System.ServiceModel.CommunicationException)
// {
// proxy.Abort();
// }
// catch (System.TimeoutException)
// {
// proxy.Abort();
// }
// catch
// {
// proxy.Abort();
// throw;
// }
//}
return g.MethodDeclaration(
asAsync ? nameTable[MemberNames.CloseProxyAsyncMethod] : nameTable[MemberNames.CloseProxyMethod],
accessibility: Accessibility.Private,
returnType: asAsync ? g.TypeExpression(compilation.RequireType<Task>()) : null,
modifiers: (asAsync ? DeclarationModifiers.Async : DeclarationModifiers.None) | DeclarationModifiers.Static,
parameters: new SyntaxNode[]
{
g.ParameterDeclaration("proxy", g.TypeExpression(compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"))),
g.ParameterDeclaration("alwaysAbort", g.TypeExpression(SpecialType.System_Boolean))
},
statements: new SyntaxNode[]
{
g.TryCatchStatement(
new SyntaxNode[]
{
// if (proxy != null && proxy.State != System.ServiceModel.CommunicationState.Closed)
g.IfStatement(
g.LogicalAndExpression(
g.ReferenceNotEqualsExpression(
g.IdentifierName("proxy"),
g.NullLiteralExpression()
),
g.ValueNotEqualsExpression(
g.MemberAccessExpression(
g.IdentifierName("proxy"),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Closed")
)
),
new SyntaxNode[]
{
// if (!alwaysAbort && proxy.State != System.ServiceModel.CommunicationState.Faulted)
g.IfStatement(
g.LogicalAndExpression(
g.LogicalNotExpression(
g.IdentifierName("alwaysAbort")
),
g.ValueNotEqualsExpression(
g.MemberAccessExpression(
g.IdentifierName("proxy"),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Faulted")
)
),
new SyntaxNode[]
{
g.ExpressionStatement(
asAsync ?
// await System.Threading.Tasks.Task.Factory.FromAsync(proxy.BeginClose, proxy.EndClose, null).ConfigureAwait(false);
AwaitExpression(g,
g.InvocationExpression(
g.DottedName("System.Threading.Tasks.Task.Factory.FromAsync"),
g.DottedName("proxy.BeginClose"),
g.DottedName("proxy.EndClose"),
g.NullLiteralExpression()
)
)
:
// proxy.Close();
g.InvocationExpression(
g.MemberAccessExpression(
g.IdentifierName("proxy"),
"Close"
)
//.........这里部分代码省略.........