本文整理汇总了C#中SyntaxGenerator.LocalDeclarationStatement方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxGenerator.LocalDeclarationStatement方法的具体用法?C# SyntaxGenerator.LocalDeclarationStatement怎么用?C# SyntaxGenerator.LocalDeclarationStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxGenerator
的用法示例。
在下文中一共展示了SyntaxGenerator.LocalDeclarationStatement方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDiagnostic
internal static SyntaxNode CreateDiagnostic(SyntaxGenerator generator, string locationName, string ruleName)
{
var identifier = generator.IdentifierName("Diagnostic");
var expression = generator.MemberAccessExpression(identifier, "Create");
SyntaxList<SyntaxNode> arguments = new SyntaxList<SyntaxNode>();
var ruleExpression = generator.IdentifierName(ruleName);
var ruleArg = generator.Argument(ruleExpression);
var locationExpression = generator.IdentifierName(locationName);
var locationArg = generator.Argument(locationExpression);
arguments = arguments.Add(ruleArg);
arguments = arguments.Add(locationArg);
string name = "diagnostic";
var initializer = generator.InvocationExpression(expression, arguments);
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(name, initializer);
return localDeclaration;
}
示例2: CreateEndOrStartSpan
//creates an end or start span statement
internal static SyntaxNode CreateEndOrStartSpan(SyntaxGenerator generator, string identifierString, string variableName)
{
SyntaxNode identifier = generator.IdentifierName(identifierString);
SyntaxNode initializer = generator.MemberAccessExpression(identifier, "SpanStart");
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(variableName, initializer);
return localDeclaration;
}
示例3: CreateOpenParen
//creates the open parenthesis statement
internal static SyntaxNode CreateOpenParen(SyntaxGenerator generator, string expressionString)
{
string name = "openParen";
var expression = generator.IdentifierName(expressionString);
var initializer = generator.MemberAccessExpression(expression, "OpenParenToken");
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(name, initializer);
return localDeclaration;
}
示例4: CreateLocation
//creates the diagnostic location statement
internal static SyntaxNode CreateLocation(SyntaxGenerator generator, string ifStatementIdentifier, string spanIdentifier)
{
string name = "diagnosticLocation";
SyntaxNode memberIdentifier = generator.IdentifierName("Location");
SyntaxNode memberName = generator.IdentifierName("Create");
SyntaxNode expression = generator.MemberAccessExpression(memberIdentifier, memberName);
SyntaxList<SyntaxNode> arguments = new SyntaxList<SyntaxNode>();
var treeIdentifier = generator.IdentifierName(ifStatementIdentifier);
var treeArgExpression = generator.MemberAccessExpression(treeIdentifier, "SyntaxTree");
var treeArg = generator.Argument(treeArgExpression);
var spanArgIdentifier = generator.IdentifierName(spanIdentifier);
var spanArg = generator.Argument(spanArgIdentifier);
arguments = arguments.Add(treeArg);
arguments = arguments.Add(spanArg);
SyntaxNode initializer = generator.InvocationExpression(expression, arguments);
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(name, initializer);
return localDeclaration;
}
示例5: CreateSpan
//creates the diagnostic span statement
internal static SyntaxNode CreateSpan(SyntaxGenerator generator, string startIdentifier, string endIdentifier)
{
string name = "diagnosticSpan";
SyntaxNode memberIdentifier = generator.IdentifierName("TextSpan");
SyntaxNode memberName = generator.IdentifierName("FromBounds");
SyntaxNode expression = generator.MemberAccessExpression(memberIdentifier, memberName);
SyntaxList<SyntaxNode> arguments = new SyntaxList<SyntaxNode>();
var startSpanIdentifier = generator.IdentifierName(startIdentifier);
var endSpanIdentifier = generator.IdentifierName(endIdentifier);
arguments = arguments.Add(startSpanIdentifier);
arguments = arguments.Add(endSpanIdentifier);
SyntaxNode initializer = generator.InvocationExpression(expression, arguments);
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(name, initializer);
return localDeclaration;
}
示例6: KeywordHelper
internal static SyntaxNode KeywordHelper(SyntaxGenerator generator, BlockSyntax methodBlock)
{
var firstStatement = methodBlock.Statements[0] as LocalDeclarationStatementSyntax;
var variableName = generator.IdentifierName(firstStatement.Declaration.Variables[0].Identifier.ValueText);
var initializer = generator.MemberAccessExpression(variableName, "IfKeyword");
var ifKeyword = generator.LocalDeclarationStatement("ifKeyword", initializer);
return ifKeyword;
}
示例7: TriviaVarMissingHelper
internal static SyntaxNode TriviaVarMissingHelper(SyntaxGenerator generator, IfStatementSyntax declaration)
{
var methodDecl = declaration.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().First();
var methodBlock = methodDecl.Body as BlockSyntax;
var secondStatement = methodBlock.Statements[1] as LocalDeclarationStatementSyntax;
var variableName = generator.IdentifierName(secondStatement.Declaration.Variables[0].Identifier.ValueText);
var ifTrailing = generator.MemberAccessExpression(variableName, "TrailingTrivia");
var fullVariable = generator.MemberAccessExpression(ifTrailing, "First");
var parameters = new SyntaxList<SyntaxNode>();
var variableExpression = generator.InvocationExpression(fullVariable, parameters);
var localDeclaration = generator.LocalDeclarationStatement("trailingTrivia", variableExpression);
return localDeclaration;
}
示例8: TriviaVarMissingHelper
// creates the first trailing trivia variable
protected internal static SyntaxNode TriviaVarMissingHelper(SyntaxGenerator generator, IfStatementSyntax declaration)
{
var methodDecl = declaration.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().First();
var methodBlock = methodDecl.Body as BlockSyntax;
string variableName = GetIfKeywordName(methodBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
SyntaxNode ifTrailing = generator.MemberAccessExpression(identifierName, "TrailingTrivia");
SyntaxNode fullVariable = generator.MemberAccessExpression(ifTrailing, "First");
var parameters = new SyntaxList<SyntaxNode>();
SyntaxNode variableExpression = generator.InvocationExpression(fullVariable, parameters);
SyntaxNode localDeclaration = generator.LocalDeclarationStatement("trailingTrivia", variableExpression);
return localDeclaration;
}
示例9: IfHelper
internal static SyntaxNode IfHelper(SyntaxGenerator generator, string name)
{
var type = SyntaxFactory.ParseTypeName("IfStatementSyntax");
var expression = generator.IdentifierName(name);
var memberAccessExpression = generator.MemberAccessExpression(expression, "Node");
var initializer = generator.CastExpression(type, memberAccessExpression);
var ifStatement = generator.LocalDeclarationStatement("ifStatement", initializer);
return ifStatement;
}
示例10: KeywordHelper
// creates the if-keyword statement
protected internal static SyntaxNode KeywordHelper(SyntaxGenerator generator, BlockSyntax methodBlock)
{
string variableName = GetIfStatementName(methodBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
SyntaxNode initializer = generator.MemberAccessExpression(identifierName, "IfKeyword");
SyntaxNode ifKeyword = generator.LocalDeclarationStatement("ifKeyword", initializer);
return ifKeyword;
}
示例11: GetMethodStatementsSyntax
private static IEnumerable<SyntaxNode> GetMethodStatementsSyntax(
SyntaxGenerator syntaxGenerator,
SemanticModel semanticModel,
IMethodSymbol methodSymbol)
{
// GENERATED CODE (for every ref or out parameter):
//
// string someOutParameter;
// var someRefParameter = default(int);
for (var i = 0; i < methodSymbol.Parameters.Length; ++i)
{
var parameter = methodSymbol.Parameters[i];
if (parameter.RefKind == RefKind.Out)
{
yield return syntaxGenerator
.LocalDeclarationStatement(
syntaxGenerator.TypeExpression(parameter.Type),
GetNameForParameter(methodSymbol, parameter));
}
else if (parameter.RefKind == RefKind.Ref)
{
yield return syntaxGenerator
.LocalDeclarationStatement(
GetNameForParameter(methodSymbol, parameter),
initializer: syntaxGenerator.DefaultExpression(syntaxGenerator.TypeExpression(parameter.Type)));
}
}
var arguments = methodSymbol
.Parameters
.Select(x =>
syntaxGenerator
.Argument(
x.RefKind,
syntaxGenerator.IdentifierName(GetNameForParameter(methodSymbol, x))))
.ToList();
var typeArguments = methodSymbol
.TypeArguments
.Select(x => syntaxGenerator.TypeExpression(x))
.ToList();
var lambdaParameterName = GetUniqueName(methodSymbol);
var lambdaInvocation = syntaxGenerator
.MemberAccessExpression(
syntaxGenerator.IdentifierName(lambdaParameterName),
methodSymbol.Name);
if (typeArguments.Count > 0)
{
lambdaInvocation = syntaxGenerator
.WithTypeArguments(
lambdaInvocation,
typeArguments);
}
// GENERATED CODE (for every ref or out parameter):
//
// someOutParameter = this.GetOutParameterValue<string>(x => x.TheMethod(out someOutParameter), parameterIndex: 0);
// someRefParameter = this.GetRefParameterValue<int>(x => x.TheMethod(ref someRefParameter), parameterIndex: 0);
for (var i = 0; i < methodSymbol.Parameters.Length; ++i)
{
var parameter = methodSymbol.Parameters[i];
if (parameter.RefKind == RefKind.Out || parameter.RefKind == RefKind.Ref)
{
var nameOfMethodToCall = parameter.RefKind == RefKind.Out ? "GetOutParameterValue" : "GetRefParameterValue";
yield return syntaxGenerator
.AssignmentStatement(
syntaxGenerator.IdentifierName(parameter.Name),
syntaxGenerator
.InvocationExpression(
syntaxGenerator.MemberAccessExpression(
syntaxGenerator.ThisExpression(),
syntaxGenerator.GenericName(
nameOfMethodToCall,
typeArguments: syntaxGenerator.TypeExpression(parameter.Type))),
arguments: new[]
{
syntaxGenerator.ValueReturningLambdaExpression(
lambdaParameterName,
syntaxGenerator.InvocationExpression(
lambdaInvocation,
arguments: arguments)),
syntaxGenerator.LiteralExpression(i)
}));
}
}
// GENERATED CODE:
//
// [return] this.Apply(x => x.SomeMethod(param1, param2));
var applyInvocation = syntaxGenerator
.InvocationExpression(
syntaxGenerator.MemberAccessExpression(
syntaxGenerator.ThisExpression(),
"Apply"),
//.........这里部分代码省略.........
示例12: 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",
//.........这里部分代码省略.........
示例13: 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()
)
}
)
}
)
}
);
}
示例14: CreateProxyVaraibleDeclaration
private SyntaxNode CreateProxyVaraibleDeclaration(SyntaxGenerator g, GenerationNameTable nameTable, bool isAsync)
{
// var proxy = this.GetProxy();
if (!isAsync)
{
return g.LocalDeclarationStatement(nameTable[MemberNames.ProxyVariable],
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
g.IdentifierName(nameTable[MemberNames.GetProxyMethod])
)
)
);
}
else
{
// var proxy = await this.GetProxyAsync().ConfigureAwait(false);
return
g.LocalDeclarationStatement(nameTable[MemberNames.ProxyVariable],
g.AwaitExpression(
g.InvocationExpression(
g.MemberAccessExpression(
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
g.IdentifierName(nameTable[MemberNames.GetProxyAsyncMethod])
)
), "ConfigureAwait"),
g.FalseLiteralExpression()
)
)
);
}
}