本文整理汇总了C#中LiteralExpressionSyntax类的典型用法代码示例。如果您正苦于以下问题:C# LiteralExpressionSyntax类的具体用法?C# LiteralExpressionSyntax怎么用?C# LiteralExpressionSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LiteralExpressionSyntax类属于命名空间,在下文中一共展示了LiteralExpressionSyntax类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: VisitLiteralExpression
public override void VisitLiteralExpression(LiteralExpressionSyntax node)
{
switch (node.Kind())
{
case SyntaxKind.TrueLiteralExpression:
VisitSymbol("true");
break;
case SyntaxKind.FalseLiteralExpression:
VisitSymbol("false");
break;
default:
throw new InvalidPreprocessorExpressionException("Expected true or false literal expression");
}
}
开发者ID:transformersprimeabcxyz,项目名称:_TO-DO-codeformatter-dotnet,代码行数:14,代码来源:PreprocessorSymbolTracker.cs
示例17: VisitLiteralExpression
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node)
{
if (node.Kind != SyntaxKind.StringLiteralExpression)
return node;
var variables = InterpolationUtils.EnumerateVariables(node.GetText().ToString()).Distinct().ToArray();
var arguments = string.Join(", ", variables);
var formatString = node.GetText().ToString();
for (int i = 0; i < variables.Length; i++) {
formatString = formatString.Replace("$" + variables[0], "{" + i + "}");
}
return Syntax.ParseExpression(string.Format("string.Format({0}, {1})", formatString, arguments));
}
开发者ID:JackWangCUMT,项目名称:RoslynStringInterpolation,代码行数:15,代码来源:StringInterpolator.cs
示例18: RewriteCharacterLiteralExpression
private static SyntaxNode RewriteCharacterLiteralExpression(LiteralExpressionSyntax node)
{
Debug.Assert(node.Kind() == SyntaxKind.CharacterLiteralExpression);
if (HasNonAsciiCharacters(node.Token.Text))
{
string convertedText = EscapeNonAsciiCharacters(node.Token.Text);
SyntaxToken t = SyntaxFactory.Literal(node.Token.LeadingTrivia, convertedText, node.Token.ValueText, node.Token.TrailingTrivia);
node = node.WithToken(t);
}
return node;
}
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:15,代码来源:NonAsciiCharactersAreEscapedInLiteralsRule.cs
示例19: GetNewLiteral
private static SyntaxToken GetNewLiteral(LiteralExpressionSyntax literal, SemanticModel semanticModel)
{
var type = semanticModel.GetTypeInfo(literal).Type;
var text = literal.Token.Text;
var reversedText = text.Reverse();
var reversedTextEnding = new string(reversedText.TakeWhile(char.IsLetter).ToArray());
var reversedTextBeginning = new string(reversedText.SkipWhile(char.IsLetter).ToArray());
var newText = new string((reversedTextEnding.ToUpperInvariant() + reversedTextBeginning).Reverse().ToArray());
switch (type.SpecialType)
{
case SpecialType.System_Int32:
return SyntaxFactory.Literal(
newText,
(int)literal.Token.Value);
case SpecialType.System_Char:
return SyntaxFactory.Literal(
newText,
(char)literal.Token.Value);
case SpecialType.System_UInt32:
return SyntaxFactory.Literal(
newText,
(uint)literal.Token.Value);
case SpecialType.System_Int64:
return SyntaxFactory.Literal(
newText,
(long)literal.Token.Value);
case SpecialType.System_UInt64:
return SyntaxFactory.Literal(
newText,
(ulong)literal.Token.Value);
case SpecialType.System_Decimal:
return SyntaxFactory.Literal(
newText,
(decimal)literal.Token.Value);
case SpecialType.System_Single:
return SyntaxFactory.Literal(
newText,
(float)literal.Token.Value);
case SpecialType.System_Double:
return SyntaxFactory.Literal(
newText,
(double)literal.Token.Value);
default:
return SyntaxFactory.Token(SyntaxKind.None);
}
}
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:47,代码来源:LiteralSuffixUpperCaseCodeFixProvider.cs
示例20: VisitLiteralExpression
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node)
{
if (node.CSharpKind() != SyntaxKind.StringLiteralExpression) return node;
var pos = node.GetLocation().GetMappedLineSpan();
var result = OnRewrite(pos.StartLinePosition.Line + 1, pos.StartLinePosition.Character + 1, Source.Lines[pos.StartLinePosition.Line].ToString(), node.Token.ValueText);
if (result == null) return node;
var names = result.Split('.');
ExpressionSyntax newNode = SyntaxFactory.IdentifierName(names.First());
foreach (var name in names.Skip(1))
{
newNode = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, newNode, SyntaxFactory.IdentifierName(name));
}
return newNode.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
}
开发者ID:GHScan,项目名称:DailyProjects,代码行数:17,代码来源:RoslynTool.cs
注:本文中的LiteralExpressionSyntax类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论