本文整理汇总了C#中Microsoft.CodeAnalysis.Document.FindAvailabeVariableName方法的典型用法代码示例。如果您正苦于以下问题:C# Document.FindAvailabeVariableName方法的具体用法?C# Document.FindAvailabeVariableName怎么用?C# Document.FindAvailabeVariableName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Document
的用法示例。
在下文中一共展示了Document.FindAvailabeVariableName方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LiteralToLambdaAsync
private async Task<Document> LiteralToLambdaAsync(Document document, InvocationExpressionSyntax invocationExpr, CancellationToken cancellationToken)
{
var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken);
var variableName = await document.FindAvailabeVariableName(invocationExpr, cancellationToken);
var expressionSyntax = invocationExpr.ArgumentList.Arguments[0].Expression;
var identifierNameSyntax = expressionSyntax as IdentifierNameSyntax;
if (identifierNameSyntax == null)
{
var literalExpression = expressionSyntax as LiteralExpressionSyntax;
var skipExpression = literalExpression != null ? literalExpression.Token.ValueText : expressionSyntax.ToString();
var lambda = SyntaxFactory.ParseStatement($"var {variableName} = {skipExpression};")
.WithAdditionalAnnotations(Formatter.Annotation).WithTrailingTrivia(SyntaxFactory.EndOfLine("\r"));
var node = invocationExpr.Parent;
while (!node.Parent.IsKind(SyntaxKind.Block))
{
node = node.Parent;
}
documentEditor.InsertBefore(node, lambda);
documentEditor.ReplaceNode(expressionSyntax, SyntaxFactory.ParseExpression($"() => {variableName}"));
}
else
{
documentEditor.ReplaceNode(expressionSyntax, SyntaxFactory.ParseExpression($"() => {identifierNameSyntax.Identifier.Text}"));
}
var newRoot = documentEditor.GetChangedRoot() as CompilationUnitSyntax;
newRoot = newRoot.AddUsings(Namespaces.System.Data.Entity);
return document.WithSyntaxRoot(newRoot);
}
开发者ID:FeodorFitsner,项目名称:Entity-Framework-Analyzers,代码行数:41,代码来源:UsePaginationWithLambdaOverloadAnalyzerCodeFixProvider.cs
示例2: LiteralToLambdaAsync
private async Task<Document> LiteralToLambdaAsync(Document document, InvocationExpressionSyntax invocationExpr, CancellationToken cancellationToken)
{
var generatedVariables = new List<string>();
var lambdaVariableName = await document.FindAvailabeVariableName(invocationExpr, cancellationToken, generatedVariables);
generatedVariables.Add(lambdaVariableName);
var argumentList = invocationExpr.ArgumentList;
var incudePath = argumentList.Arguments[0].Expression;
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var method = semanticModel.GetSymbolInfo(invocationExpr).Symbol as IMethodSymbol;
var underlyingType = (method?.ReceiverType as INamedTypeSymbol)?.TypeArguments[0];
var paths = incudePath.ToFullString().Trim('"').Split('.');
var lambdaPath = $"{lambdaVariableName} => {lambdaVariableName}";
var nestedLevels = 0;
var previousPropertyIsCollection = false;
foreach (var path in paths)
{
var property = underlyingType?.GetMembers(path).SingleOrDefault(symbol => symbol.Kind == SymbolKind.Property) as IPropertySymbol;
if (property == null)
{
return document;
}
lambdaPath += ".";
if (previousPropertyIsCollection)
{
var innerLambdaVariableName = await document.FindAvailabeVariableName(invocationExpr, cancellationToken, generatedVariables);
generatedVariables.Add(innerLambdaVariableName);
lambdaPath += $"Select({innerLambdaVariableName}=>{innerLambdaVariableName}.{path}";
nestedLevels++;
}
else
{
lambdaPath += path;
}
previousPropertyIsCollection = property.Type.AllInterfaces.Any(x => x.MetadataName == typeof(IEnumerable<>).Name);
// If the property is List<T> or ICollection<T> get the underlying type for next iteration.
if (previousPropertyIsCollection)
{
underlyingType = (property.Type as INamedTypeSymbol)?.TypeArguments[0];
}
}
lambdaPath += new string(')', nestedLevels);
var lambdaExpression = SyntaxFactory.ParseExpression(lambdaPath)
.WithAdditionalAnnotations(Formatter.Annotation);
var stringLiteralExpression = invocationExpr.ArgumentList.Arguments[0].Expression;
var root = await document.GetSyntaxRootAsync(cancellationToken) as CompilationUnitSyntax;
var newRoot = root.ReplaceNode(stringLiteralExpression, lambdaExpression);
newRoot = newRoot.AddUsings(Namespaces.System.Data.Entity);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
开发者ID:FeodorFitsner,项目名称:Entity-Framework-Analyzers,代码行数:71,代码来源:UseIncludeWithLambdaAnalyzerCodeFixProvider.cs