当前位置: 首页>>代码示例>>C#>>正文


C# LiteralExpressionSyntax类代码示例

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


LiteralExpressionSyntax类属于命名空间,在下文中一共展示了LiteralExpressionSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetParameterNameThatMatchStringLiteral

 private static string GetParameterNameThatMatchStringLiteral(LiteralExpressionSyntax stringLiteral)
 {
     var ancestorThatMightHaveParameters = stringLiteral.FirstAncestorOfType(typeof(AttributeListSyntax), typeof(MethodDeclarationSyntax), typeof(ConstructorDeclarationSyntax), typeof(IndexerDeclarationSyntax));
     var parameterName = string.Empty;
     if (ancestorThatMightHaveParameters != null)
     {
         var parameters = new SeparatedSyntaxList<ParameterSyntax>();
         switch (ancestorThatMightHaveParameters.Kind())
         {
             case SyntaxKind.MethodDeclaration:
             case SyntaxKind.ConstructorDeclaration:
                 var method = (BaseMethodDeclarationSyntax)ancestorThatMightHaveParameters;
                 parameters = method.ParameterList.Parameters;
                 break;
             case SyntaxKind.IndexerDeclaration:
                 var indexer = (IndexerDeclarationSyntax)ancestorThatMightHaveParameters;
                 parameters = indexer.ParameterList.Parameters;
                 break;
             case SyntaxKind.AttributeList:
                 break;
         }
         parameterName = GetParameterWithIdentifierEqualToStringLiteral(stringLiteral, parameters)?.Identifier.Text;
     }
     return parameterName;
 }
开发者ID:nikolasmagno,项目名称:code-cracker,代码行数:25,代码来源:NameOfAnalyzer.cs

示例2: CreateNewLiteral

 private static SyntaxNode CreateNewLiteral(LiteralExpressionSyntax literal, SyntaxNode root)
 {
     SyntaxNode newRoot;
     if (literal.Token.ValueText != literal.Token.Text)
     {
         var newTokenText = literal.Token.ValueText;
         var newLiteral = literal.WithToken(SyntaxFactory.ParseToken(newTokenText));
         newRoot = root.ReplaceNode(literal, newLiteral);
     }
     else
     {
         var value = (dynamic)literal.Token.Value;
         if (literal.Parent != null && literal.Parent.IsKind(SyntaxKind.UnaryMinusExpression))
         {
             var newTokenText = (string)("0x" + (value * -1).ToString("X"));
             var newLiteral = literal.WithToken(SyntaxFactory.ParseToken(newTokenText))
                 .WithLeadingTrivia(literal.Parent.GetLeadingTrivia())
                 .WithTrailingTrivia(literal.Parent.GetTrailingTrivia());
             newRoot = root.ReplaceNode(literal.Parent, newLiteral);
         }
         else
         {
             var newTokenText = (string)("0x" + value.ToString("X"));
             var newLiteral = literal.WithToken(SyntaxFactory.ParseToken(newTokenText));
             newRoot = root.ReplaceNode(literal, newLiteral);
         }
     }
     return newRoot;
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:29,代码来源:NumericLiteralCodeFixProvider.cs

示例3: FormatXmlAsync

		private async Task<Document> FormatXmlAsync(Document document, LiteralExpressionSyntax sleSyntax, CancellationToken cancellationToken)
		{
			var root = await document.GetSyntaxRootAsync(cancellationToken);
			var tree = root.SyntaxTree;

			//Get the character position of the LiteralExpressionSyntax
			FileLinePositionSpan position = tree.GetLineSpan(sleSyntax.Span);
			int cSpace = position.StartLinePosition.Character;

			//Figure out the preceeding trivia since we can't get the column position from GetLineSpan
			var parentTrivia = sleSyntax.GetLeadingTrivia().ToFullString();

			var xml = sleSyntax.GetFirstToken().ValueText;
			var newXmlText = FormatXml(xml);

			//Process each line of the formatted XML and prepend the parent trivia & spaces
			string[] xmlLines = newXmlText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
			for (int i = 1; i < xmlLines.Length; i++)
			{
				xmlLines[i] = parentTrivia + new String(' ', (cSpace + 2)) + xmlLines[i];
			}

			newXmlText = String.Join("\r\n", xmlLines);
			var newXmlValue = "@\"" + newXmlText + "\"";

			var newNode = SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
				SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.StringLiteralToken, newXmlValue, newXmlText, SyntaxTriviaList.Empty));
			var newRoot = root.ReplaceNode(sleSyntax, newNode);
			return document.WithSyntaxRoot(newRoot);
		}
开发者ID:modulexcite,项目名称:GoingBeyondReSharperWithRoslyn_Demo,代码行数:30,代码来源:CodeRefactoringProvider.cs

示例4: StringInterpolation

        public StringInterpolation(SemanticModel model, LiteralExpressionSyntax node, bool constant)
        {
            this.model = model;
            this.node = node;
            this.constant = constant;

            literal  = node.Token.ValueText;
        }
开发者ID:jthelin,项目名称:Nake,代码行数:8,代码来源:StringInterpolation.cs

示例5: UseStringDotEmptyAsync

        private static Task<Solution> UseStringDotEmptyAsync(Document document, SyntaxNode root, LiteralExpressionSyntax literalDeclaration)
        {
            var stringDotEmptyInvocation = SyntaxFactory.ParseExpression("string.Empty");
            var newRoot = root.ReplaceNode(literalDeclaration, stringDotEmptyInvocation);

            var newDocument = document.WithSyntaxRoot(newRoot);
            return Task.FromResult(newDocument.Project.Solution);
        }
开发者ID:nemec,项目名称:VSDiagnostics,代码行数:8,代码来源:ReplaceEmptyStringWithStringDotEmptyCodeFix.cs

示例6: VisitLiteralExpression

        public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node)
        {
            if (node.IsKind(SyntaxKind.StringLiteralExpression)
                && node.ToString().IndexOf('\n') >= 0)
            {
                node = node.WithAdditionalAnnotations(LayoutAnnotations.MultiLineConstructAnnotation);
            }

            return base.VisitLiteralExpression(node);
        }
开发者ID:madelson,项目名称:MedallionCodeFormatter,代码行数:10,代码来源:MultiLineConstructAnnotator.cs

示例7: ToVerbatimStringLiteral

 private static ExpressionSyntax ToVerbatimStringLiteral(LiteralExpressionSyntax expression)
 {
     var str = (string)expression.Token.Value;
     return LiteralExpression(SyntaxKind.StringLiteralExpression,
         Literal(
             TriviaList(),
             StringToVerbatimText(str),
             str,
             TriviaList()));
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:10,代码来源:StringRepresentationCodeFixProvider.cs

示例8: GetIdentifierAsync

 private static async Task<string> GetIdentifierAsync(Document document, LiteralExpressionSyntax stringLiteral, SyntaxNode root, Diagnostic diagnostic, CancellationToken cancellationToken)
 {
     var diagnosticSpan = diagnostic.Location.SourceSpan;
     var parameter = FindParameterThatStringLiteralRefers(root, diagnosticSpan.Start, stringLiteral);
     if (parameter != null)
         return parameter.Identifier.Text;
     var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
     var symbol = semanticModel.LookupSymbols(stringLiteral.Token.SpanStart, null, stringLiteral.Token.ValueText).First();
     return symbol.ToDisplayParts().Last(NameOfAnalyzer.IncludeOnlyPartsThatAreName).ToString();
 }
开发者ID:nagyistoce,项目名称:code-cracker,代码行数:10,代码来源:NameOfCodeFixProvider.cs

示例9: IsBoolean

 static bool IsBoolean(LiteralExpressionSyntax expr, out bool value)
 {
     value = false;
     if (expr != null && (expr.Token.Value is bool))
     {
         value = (bool)expr.Token.Value;
         return true;
     }
     return false;
 }
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:10,代码来源:RedundantTernaryExpressionAnalyzer.cs

示例10: VisitLiteralExpression

        public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node)
        {
            var interpolation = result.Find(node);
            if (interpolation == null)
                return base.VisitLiteralExpression(node);

            var interpolated = interpolation.Interpolate();
            foreach (var variable in interpolation.Captured)
                Captured.Add(variable);

            return interpolated;
        }
开发者ID:jthelin,项目名称:Nake,代码行数:12,代码来源:Rewriter.cs

示例11: VisitLiteralExpression

            public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node)
            {
                switch (node.Kind())
                {
                    case SyntaxKind.StringLiteralExpression:
                        return RewriteStringLiteralExpression(node);
                    case SyntaxKind.CharacterLiteralExpression:
                        return RewriteCharacterLiteralExpression(node);
                }

                return node;
            }
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:12,代码来源:NonAsciiCharactersAreEscapedInLiteralsRule.cs

示例12: OutSideOfDeclarationSideWithSameName

        private static bool OutSideOfDeclarationSideWithSameName(LiteralExpressionSyntax stringLiteral)
        {
            var variableDeclaration = stringLiteral.FirstAncestorOfType<VariableDeclaratorSyntax>();
            if (variableDeclaration != null)
            {
                return !string.Equals(variableDeclaration.Identifier.ValueText, stringLiteral.Token.ValueText, StringComparison.Ordinal);
            }

            var propertyDeclaration = stringLiteral.FirstAncestorOfType<PropertyDeclarationSyntax>();
            var outSideOfAccessors = null == stringLiteral.FirstAncestorOfType<AccessorListSyntax>();
            if (!outSideOfAccessors) return true;
            return !string.Equals(propertyDeclaration?.Identifier.ValueText, stringLiteral.Token.ValueText, StringComparison.Ordinal);
        }
开发者ID:nikolasmagno,项目名称:code-cracker,代码行数:13,代码来源:NameOfAnalyzer.cs

示例13: CheckJsonValue

 private static void CheckJsonValue(SyntaxNodeAnalysisContext context, LiteralExpressionSyntax literalParameter,
     string json)
 {
     try
     {
         parseMethodInfo.Value.Invoke(null, new[] { json });
     }
     catch (Exception ex)
     {
         var diag = Diagnostic.Create(Rule, literalParameter.GetLocation(), ex.InnerException.Message);
         context.ReportDiagnostic(diag);
     }
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:13,代码来源:JsonNetAnalyzer.cs

示例14: GetProgramElementNameThatMatchStringLiteral

        private static string GetProgramElementNameThatMatchStringLiteral(LiteralExpressionSyntax stringLiteral, SemanticModel semanticModel)
        {
            var programElementName = GetParameterNameThatMatchStringLiteral(stringLiteral);

            if (!Found(programElementName))
            {
                var literalValueText = stringLiteral.Token.ValueText;
                var symbol = semanticModel.LookupSymbols(stringLiteral.Token.SpanStart, null, literalValueText).FirstOrDefault();

                programElementName = symbol?.ToDisplayParts().LastOrDefault(IncludeOnlyPartsThatAreName).ToString();
            }

            return programElementName;
        }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:14,代码来源:NameOfAnalyzer.cs

示例15: ReplaceWithNameOf

        private async Task<Document> ReplaceWithNameOf(Document document, LiteralExpressionSyntax literalExpression, CancellationToken cancellationToken)
        {
            var stringText = literalExpression.Token.ValueText;

            var nameOfExpression = InvocationExpression(
                expression: IdentifierName("nameof"),
                argumentList: ArgumentList(
                    arguments: SingletonSeparatedList(Argument(IdentifierName(stringText)))));

            var root = await document.GetSyntaxRootAsync(cancellationToken);
            var newRoot = root.ReplaceNode(literalExpression, nameOfExpression);

            return document.WithSyntaxRoot(newRoot);
        }
开发者ID:jmarolf,项目名称:CSharpEssentials,代码行数:14,代码来源:UseNameOfCodeFix.cs


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