• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Syntax.LocalDeclarationStatementSyntax类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax的典型用法代码示例。如果您正苦于以下问题:C# LocalDeclarationStatementSyntax类的具体用法?C# LocalDeclarationStatementSyntax怎么用?C# LocalDeclarationStatementSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



LocalDeclarationStatementSyntax类属于Microsoft.CodeAnalysis.CSharp.Syntax命名空间,在下文中一共展示了LocalDeclarationStatementSyntax类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16: VisitLocalDeclarationStatement

        public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            var symbolInfo = model.GetSymbolInfo(node.Declaration.Type);
            var typeSymbol = symbolInfo.Symbol;
            var type = typeSymbol.ToDisplayString(
              SymbolDisplayFormat.MinimallyQualifiedFormat);

            var declaration = SyntaxFactory
                .LocalDeclarationStatement(
                    SyntaxFactory
                        .VariableDeclaration(SyntaxFactory.IdentifierName(
                          SyntaxFactory.Identifier(type)))
                            .WithVariables(node.Declaration.Variables)
                            .NormalizeWhitespace()
                    )
                    .WithTriviaFrom(node);
            return declaration;
        }
开发者ID:dgalizzi,项目名称:RoslynVarRewriter,代码行数:18,代码来源:Program.cs


示例17: MakeConstAsync

        private async Task<Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken)
        {
            var firstToken = localDeclaration.GetFirstToken();
            var leadingTrivia = firstToken.LeadingTrivia;
            var trimmedLocal = localDeclaration.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty));

            var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker));

            var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken);

            var variableDeclaration = localDeclaration.Declaration;
            var variableTypeName = variableDeclaration.Type;
            if (variableTypeName.IsVar)
            {
                var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

                var aliasInfo = semanticModel.GetAliasInfo(variableTypeName);
                if (aliasInfo == null)
                {
                    var type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;

                    if (type.Name != "var")
                    {
                        var typeName = SyntaxFactory.ParseTypeName(type.ToDisplayString())
                            .WithLeadingTrivia(variableTypeName.GetLeadingTrivia())
                            .WithTrailingTrivia(variableTypeName.GetTrailingTrivia());

                        var simplifiedTypeName = typeName.WithAdditionalAnnotations(Simplifier.Annotation);

                        variableDeclaration = variableDeclaration.WithType(simplifiedTypeName);
                    }
                }
            }

            var newLocal = trimmedLocal.WithModifiers(newModifiers)
                                       .WithDeclaration(variableDeclaration);

            var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation);

            var root = await document.GetSyntaxRootAsync(cancellationToken);
            var newRoot = root.ReplaceNode(localDeclaration, formattedLocal);

            return document.WithSyntaxRoot(newRoot);
        }
开发者ID:julid29,项目名称:confsamples,代码行数:44,代码来源:CodeFixProvider.cs


示例18: Go

        public static void Go(OutputWriter writer, LocalDeclarationStatementSyntax declaration)
        {
            foreach (var variable in declaration.Declaration.Variables)
            {


                writer.WriteIndent();
               
                {
                    try
                    {
                        var str = TypeProcessor.ConvertType(declaration.Declaration.Type);
                        //This fails sometimes
                        // if (str == "NObject") // Dlangs casting is slow 
                        // Looks harmless but is actually a performance optimization ... makes CastTest improve by a whole lot
                        //   writer.Write("auto ");//Kills BoxingTest unfortunately as boxed types are also used to make unbound generics work :(
                        //else
                        writer.Write(str);
                    }
                    catch (Exception ex)
                    {
                        writer.Write("auto");   
                    }
                   

                    writer.Write(" ");



                    writer.Write(WriteIdentifierName.TransformIdentifier(variable.Identifier.Text));
                    writer.Write(" = ");

                     WriteInitializer(writer, declaration, variable);
                }

                writer.Write(";\r\n");
            }
        }
开发者ID:mortezabarzkar,项目名称:SharpNative,代码行数:38,代码来源:WriteLocalDeclaration.cs


示例19: BindDeclarationStatement

        private BoundStatement BindDeclarationStatement(LocalDeclarationStatementSyntax node, DiagnosticBag diagnostics)
        {
            var typeSyntax = node.Declaration.Type;
            bool isConst = node.IsConst;

            bool isVar;
            AliasSymbol alias;
            TypeSymbol declType = BindVariableType(node, diagnostics, typeSyntax, ref isConst, isVar: out isVar, alias: out alias);

            // UNDONE: "possible expression" feature for IDE

            LocalDeclarationKind kind = LocalDeclarationKind.RegularVariable;
            if (isConst)
            {
                kind = LocalDeclarationKind.Constant;
            }

            var variableList = node.Declaration.Variables;
            int variableCount = variableList.Count;

            if (variableCount == 1)
            {
                return BindVariableDeclaration(kind, isVar, variableList[0], typeSyntax, declType, alias, diagnostics, node);
            }
            else
            {
                BoundLocalDeclaration[] boundDeclarations = new BoundLocalDeclaration[variableCount];

                int i = 0;
                foreach (var variableDeclaratorSyntax in variableList)
                {
                    boundDeclarations[i++] = BindVariableDeclaration(kind, isVar, variableDeclaratorSyntax, typeSyntax, declType, alias, diagnostics);
                }

                return new BoundMultipleLocalDeclarations(node, boundDeclarations.AsImmutableOrNull());
            }
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:37,代码来源:Binder_Statements.cs


示例20: VisitLocalDeclarationStatement

        public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            if (node.Declaration.Variables.Count == 1)
            {
                var variable = node
                    .Declaration
                    .Variables[0];

                if (variable.Initializer != null)
                {
                    var call = variable.Initializer.Value as InvocationExpressionSyntax;
                    if (call != null)
                    {
                        SyntacticalExtension<SyntaxNode> extension = codeExtension(call);
                        if (extension != null)
                        {

                            if (extension.Kind != ExtensionKind.Code)
                            {
                                //td: error, incorrect extension (i.e. a code extension being used inside a type)
                                return node;
                            }

                            _lookahead = CheckCodeExtension(node, extension);
                            return null;
                        }
                    }
                }
            }

            return base.VisitLocalDeclarationStatement(node);
        }
开发者ID:mpmedia,项目名称:Excess,代码行数:32,代码来源:ExtensionRewriter.cs



注:本文中的Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Syntax.MethodDeclarationSyntax类代码示例发布时间:2022-05-26
下一篇:
C# Syntax.IndexerDeclarationSyntax类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap