本文整理汇总了C#中SyntaxGenerator.NullLiteralExpression方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxGenerator.NullLiteralExpression方法的具体用法?C# SyntaxGenerator.NullLiteralExpression怎么用?C# SyntaxGenerator.NullLiteralExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxGenerator
的用法示例。
在下文中一共展示了SyntaxGenerator.NullLiteralExpression方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
}
示例2: 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()
)
}
)
}
)
}
);
}
示例3: 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",
//.........这里部分代码省略.........
示例4: 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"
)
//.........这里部分代码省略.........