當前位置: 首頁>>代碼示例>>C#>>正文


C# Syntax.LocalDeclarationStatementSyntax類代碼示例

本文整理匯總了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;
        }
開發者ID:haroldhues,項目名稱:code-cracker,代碼行數:27,代碼來源:MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs

示例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;
            }
        }
開發者ID:julid29,項目名稱:confsamples,代碼行數:27,代碼來源:TypeInferenceRewriter.cs

示例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);
     }
 }
開發者ID:tvsonar,項目名稱:roslyn,代碼行數:7,代碼來源:ExpressionVariableFinder.cs

示例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;
 }
開發者ID:haroldhues,項目名稱:code-cracker,代碼行數:8,代碼來源:MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs

示例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);
        }
開發者ID:gerryaobrien,項目名稱:code-cracker,代碼行數:8,代碼來源:RemoveUnusedVariablesCodeFixProvider.cs

示例7: VarIsAppropriate

        private bool VarIsAppropriate(LocalDeclarationStatementSyntax decl)
        {
            foreach (var assignment in decl.Declaration.Variables)
            {
            }

            return false;
        }
開發者ID:jimdeselms,項目名稱:codeformatter,代碼行數:8,代碼來源:VarUsageValidationRule.cs

示例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;
        }
開發者ID:pgrefviau,項目名稱:RefactoringEssentials,代碼行數:11,代碼來源:SuggestUseVarKeywordEvidentAnalyzer.cs

示例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;
            }
開發者ID:RoryVL,項目名稱:roslyn,代碼行數:13,代碼來源:MoveDeclarationNearReferenceCodeRefactoringProvider.State.cs

示例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;
        }
開發者ID:yeaicc,項目名稱:RefactoringEssentials,代碼行數:14,代碼來源:SuggestUseVarKeywordEvidentAnalyzer.cs

示例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)*/;
        }
開發者ID:Kavignon,項目名稱:RefactoringEssentials,代碼行數:16,代碼來源:SuggestUseVarKeywordEvidentAnalyzer.cs

示例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);
		}
開發者ID:srikanthdasari,項目名稱:elasticsearch-net,代碼行數:17,代碼來源:DocumentationFileWalker.cs

示例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;
        }
開發者ID:ehsansajjad465,項目名稱:roslyn,代碼行數:45,代碼來源:MethodXmlBuilder.cs

示例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());
            }
        }
開發者ID:mortezabarzkar,項目名稱:SharpNative,代碼行數:18,代碼來源:YieldStateGenerator.cs


注:本文中的Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。