本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax类的典型用法代码示例。如果您正苦于以下问题:C# LocalDeclarationStatementSyntax类的具体用法?C# LocalDeclarationStatementSyntax怎么用?C# LocalDeclarationStatementSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LocalDeclarationStatementSyntax类属于Microsoft.CodeAnalysis.CSharp.Syntax命名空间,在下文中一共展示了LocalDeclarationStatementSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsDeclarationConstFriendly
static bool IsDeclarationConstFriendly(LocalDeclarationStatementSyntax declaration, SemanticModel semanticModel)
{
// all variables could be const?
foreach (var variable in declaration.Declaration.Variables)
{
if (variable.Initializer == null) return false;
if (variable.Initializer.Value is InterpolatedStringExpressionSyntax) return false;
// is constant
var constantValue = semanticModel.GetConstantValue(variable.Initializer.Value);
var valueIsConstant = constantValue.HasValue;
if (!valueIsConstant) return false;
// if reference type, value is null?
var variableTypeName = declaration.Declaration.Type;
var variableType = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;
if (variableType.IsReferenceType && variableType.SpecialType != SpecialType.System_String && constantValue.Value != null) return false;
// nullable?
if (variableType.OriginalDefinition?.SpecialType == SpecialType.System_Nullable_T) return false;
// value can be converted to variable type?
var conversion = semanticModel.ClassifyConversion(variable.Initializer.Value, variableType);
if (!conversion.Exists || conversion.IsUserDefined) return false;
}
return true;
}
示例2: VisitLocalDeclarationStatement
public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
{
if (node.Declaration.Variables.Count > 1)
return node;
if (node.Declaration.Variables[0].Initializer == null)
return node;
VariableDeclaratorSyntax declarator = node.Declaration.Variables.First();
TypeSyntax variableTypeName = node.Declaration.Type;
ITypeSymbol variableType = (ITypeSymbol)SemanticModel.GetSymbolInfo(variableTypeName).Symbol;
TypeInfo initializerInfo = SemanticModel.GetTypeInfo(declarator.Initializer.Value);
if (variableType == initializerInfo.Type)
{
TypeSyntax varTypeName = SyntaxFactory.IdentifierName("var")
.WithLeadingTrivia(variableTypeName.GetLeadingTrivia())
.WithTrailingTrivia(variableTypeName.GetTrailingTrivia());
return node.ReplaceNode(variableTypeName, varTypeName);
}
else
{
return node;
}
}
示例3: IntroduceLocalDeclarationIntoLambda
private Document IntroduceLocalDeclarationIntoLambda(
SemanticDocument document,
ExpressionSyntax expression,
IdentifierNameSyntax newLocalName,
LocalDeclarationStatementSyntax declarationStatement,
SyntaxNode oldLambda,
bool allOccurrences,
CancellationToken cancellationToken)
{
var oldBody = oldLambda is ParenthesizedLambdaExpressionSyntax
? (ExpressionSyntax)((ParenthesizedLambdaExpressionSyntax)oldLambda).Body
: (ExpressionSyntax)((SimpleLambdaExpressionSyntax)oldLambda).Body;
var rewrittenBody = Rewrite(
document, expression, newLocalName, document, oldBody, allOccurrences, cancellationToken);
var delegateType = document.SemanticModel.GetTypeInfo(oldLambda, cancellationToken).ConvertedType as INamedTypeSymbol;
var newBody = delegateType != null && delegateType.DelegateInvokeMethod != null && delegateType.DelegateInvokeMethod.ReturnsVoid
? SyntaxFactory.Block(declarationStatement)
: SyntaxFactory.Block(declarationStatement, SyntaxFactory.ReturnStatement(rewrittenBody));
newBody = newBody.WithAdditionalAnnotations(Formatter.Annotation);
var newLambda = oldLambda is ParenthesizedLambdaExpressionSyntax
? ((ParenthesizedLambdaExpressionSyntax)oldLambda).WithBody(newBody)
: (SyntaxNode)((SimpleLambdaExpressionSyntax)oldLambda).WithBody(newBody);
var newRoot = document.Root.ReplaceNode(oldLambda, newLambda);
return document.Document.WithSyntaxRoot(newRoot);
}
开发者ID:pabloescribanoloza,项目名称:monodevelop,代码行数:31,代码来源:CSharpIntroduceVariableService_IntroduceLocal.cs
示例4: VisitLocalDeclarationStatement
public override void VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
{
foreach (var decl in node.Declaration.Variables)
{
Visit(decl.Initializer?.Value);
}
}
示例5: AreVariablesOnlyWrittenInsideDeclaration
static bool AreVariablesOnlyWrittenInsideDeclaration(LocalDeclarationStatementSyntax declaration, SemanticModel semanticModel)
{
var dfa = semanticModel.AnalyzeDataFlow(declaration);
var symbols = from variable in declaration.Declaration.Variables
select semanticModel.GetDeclaredSymbol(variable);
var result = !symbols.Any(s => dfa.WrittenOutside.Contains(s));
return result;
}
示例6: RemoveVariableAsync
private async static Task<Document> RemoveVariableAsync(Document document, LocalDeclarationStatementSyntax variableUnused, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.RemoveNode(variableUnused, SyntaxRemoveOptions.KeepNoTrivia);
return document.WithSyntaxRoot(newRoot);
}
示例7: VarIsAppropriate
private bool VarIsAppropriate(LocalDeclarationStatementSyntax decl)
{
foreach (var assignment in decl.Declaration.Variables)
{
}
return false;
}
示例8: TryValidateLocalVariableType
static bool TryValidateLocalVariableType(LocalDeclarationStatementSyntax localDeclarationStatementSyntax, VariableDeclarationSyntax variableDeclarationSyntax)
{
//Either we don't have a local variable or we're using constant value
if (localDeclarationStatementSyntax == null ||
localDeclarationStatementSyntax.IsConst ||
localDeclarationStatementSyntax.ChildNodes().OfType<VariableDeclarationSyntax>().Count() != 1)
return false;
//We don't want to raise a diagnostic if the local variable is already a var
return !variableDeclarationSyntax.Type.IsVar;
}
示例9: Rewriter
public Rewriter(
BlockSyntax oldInnermostBlock,
BlockSyntax newInnermostBlock,
BlockSyntax oldOutermostBlock,
LocalDeclarationStatementSyntax declarationStatement)
{
_oldInnermostBlock = oldInnermostBlock;
_newInnermostBlock = newInnermostBlock;
_oldOutermostBlock = oldOutermostBlock;
_declarationStatement = declarationStatement;
}
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:11,代码来源:MoveDeclarationNearReferenceCodeRefactoringProvider.Rewriter.cs
示例10: GenerateAsync
internal static async Task<State> GenerateAsync(
SemanticDocument document,
LocalDeclarationStatementSyntax statement,
CancellationToken cancellationToken)
{
var state = new State();
if (!await state.TryInitializeAsync(document, statement, cancellationToken).ConfigureAwait(false))
{
return null;
}
return state;
}
示例11: IsArrayTypeSomeObviousTypeCase
static bool IsArrayTypeSomeObviousTypeCase(SyntaxNodeAnalysisContext nodeContext, ExpressionSyntax initializerExpression, ITypeSymbol variableType, LocalDeclarationStatementSyntax localVariable)
{
var arrayCreationExpressionSyntax = initializerExpression as ArrayCreationExpressionSyntax;
if (arrayCreationExpressionSyntax != null)
{
if (arrayCreationExpressionSyntax.Type.IsMissing)
return false;
var arrayType = nodeContext.SemanticModel.GetTypeInfo(arrayCreationExpressionSyntax).Type;
return arrayType != null && arrayCreationExpressionSyntax.Initializer != null && variableType.Equals(arrayType);
}
return false;
}
示例12: DidVariableDeclarationTypeCorrespondToObviousCase
static bool DidVariableDeclarationTypeCorrespondToObviousCase(SyntaxNodeAnalysisContext nodeContext, LocalDeclarationStatementSyntax localVariable)
{
var singleVariable = localVariable.Declaration.Variables.First();
var initializer = singleVariable.Initializer;
if (initializer == null)
return false;
var initializerExpression = initializer.Value;
var variableTypeName = localVariable.Declaration.Type;
var semanticModel = nodeContext.SemanticModel;
var variableType = semanticModel.GetSymbolInfo(variableTypeName, nodeContext.CancellationToken).Symbol as ITypeSymbol;
return IsArrayTypeSomeObviousTypeCase(nodeContext, initializerExpression, variableType, localVariable) ||
IsObjectCreationSomeObviousTypeCase(nodeContext, initializerExpression, variableType) ||
IsCastingSomeObviousTypeCase(nodeContext, initializerExpression, variableType) /*||
IsPropertyAccessSomeObviousTypeCase(nodeContext, initializerExpression, variableType)*/;
}
示例13: VisitLocalDeclarationStatement
public override void VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
{
if (this.InsideAutoIncludeMethodBlock)
{
var allchildren = node.DescendantNodesAndTokens(descendIntoTrivia: true);
var line = node.SyntaxTree.GetLineSpan(node.Span).StartLinePosition.Line;
if (allchildren.Any(a => a.Kind() == SyntaxKind.MultiLineDocumentationCommentTrivia))
{
var walker = new CodeWithDocumentationWalker(ClassDepth, line);
walker.Visit(node.WithAdditionalAnnotations());
this.Blocks.AddRange(walker.Blocks);
return;
}
this.Blocks.Add(new CodeBlock(node.WithoutLeadingTrivia().ToFullString(), line));
}
base.VisitLocalDeclarationStatement(node);
}
示例14: TryGenerateLocal
private bool TryGenerateLocal(LocalDeclarationStatementSyntax localDeclarationStatement)
{
/*
- <ElementType name="Local" content="eltOnly">
<attribute type="id" />
<attribute type="static" />
<attribute type="instance" />
<attribute type="implicit" />
<attribute type="constant" />
- <group order="one">
<element type="Type" />
<element type="ArrayType" />
</group>
- <group minOccurs="1" maxOccurs="*" order="seq">
<element type="LocalName" />
<element type="Expression" minOccurs="0" maxOccurs="1" />
</group>
</ElementType>
*/
using (LocalTag(GetLineNumber(localDeclarationStatement)))
{
// Spew the type first
if (!TryGenerateType(localDeclarationStatement.Declaration.Type))
{
return false;
}
// Now spew the list of variables
foreach (var variable in localDeclarationStatement.Declaration.Variables)
{
GenerateName(variable.Identifier.ToString());
if (variable.Initializer != null)
{
if (!TryGenerateExpression(variable.Initializer.Value))
{
return false;
}
}
}
}
return true;
}
示例15: VisitLocalDeclarationStatement
public override void VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
{
var semanticModel = compilation.GetSemanticModel(node.SyntaxTree);
// Convert the variable declaration to an assignment expression statement
foreach (var variable in node.Declaration.Variables)
{
if (variable.Initializer != null)
{
var assignment = SyntaxFactory.IdentifierName(variable.Identifier.ToString()).Assign(StateMachineThisFixer.Fix(variable.Initializer.Value));
currentState.Add(Cs.Express(assignment));
}
// Hoist the variable into a field
var symbol = (ILocalSymbol)ModelExtensions.GetDeclaredSymbol(semanticModel, variable);
node = (LocalDeclarationStatementSyntax)HoistVariable(node, variable.Identifier, symbol.Type.ToTypeSyntax());
}
}