本文整理汇总了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;
}
示例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;
}
示例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);
}
示例4: StringInterpolation
public StringInterpolation(SemanticModel model, LiteralExpressionSyntax node, bool constant)
{
this.model = model;
this.node = node;
this.constant = constant;
literal = node.Token.ValueText;
}
示例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);
}
示例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);
}
示例7: ToVerbatimStringLiteral
private static ExpressionSyntax ToVerbatimStringLiteral(LiteralExpressionSyntax expression)
{
var str = (string)expression.Token.Value;
return LiteralExpression(SyntaxKind.StringLiteralExpression,
Literal(
TriviaList(),
StringToVerbatimText(str),
str,
TriviaList()));
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
}
示例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;
}
示例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);
}