本文整理汇总了C#中IFieldSymbol.GetSymbolKey方法的典型用法代码示例。如果您正苦于以下问题:C# IFieldSymbol.GetSymbolKey方法的具体用法?C# IFieldSymbol.GetSymbolKey怎么用?C# IFieldSymbol.GetSymbolKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFieldSymbol
的用法示例。
在下文中一共展示了IFieldSymbol.GetSymbolKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EncapsulateFieldAsync
private async Task<Result> EncapsulateFieldAsync(IFieldSymbol field, Document document, bool updateReferences, CancellationToken cancellationToken)
{
var originalField = field;
var finalNames = GeneratePropertyAndFieldNames(field);
var finalFieldName = finalNames.Item1;
var generatedPropertyName = finalNames.Item2;
// Annotate the field declarations so we can find it after rename.
var fieldDeclaration = field.DeclaringSyntaxReferences.First();
var declarationAnnotation = new SyntaxAnnotation();
document = document.WithSyntaxRoot(fieldDeclaration.SyntaxTree.GetRoot(cancellationToken).ReplaceNode(fieldDeclaration.GetSyntax(cancellationToken),
fieldDeclaration.GetSyntax(cancellationToken).WithAdditionalAnnotations(declarationAnnotation)));
var solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
var linkedDocument = solution.GetDocument(linkedDocumentId);
var linkedRoot = await linkedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var linkedFieldNode = linkedRoot.FindNode(fieldDeclaration.Span);
if (linkedFieldNode.Span != fieldDeclaration.Span)
{
continue;
}
var updatedRoot = linkedRoot.ReplaceNode(linkedFieldNode, linkedFieldNode.WithAdditionalAnnotations(declarationAnnotation));
solution = solution.WithDocumentSyntaxRoot(linkedDocumentId, updatedRoot);
}
document = solution.GetDocument(document.Id);
// Resolve the annotated symbol and prepare for rename.
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var compilation = semanticModel.Compilation;
field = field.GetSymbolKey().Resolve(compilation, cancellationToken: cancellationToken).Symbol as IFieldSymbol;
var solutionNeedingProperty = solution;
// We couldn't resolve field after annotating its declaration. Bail
if (field == null)
{
return null;
}
solutionNeedingProperty = await UpdateReferencesAsync(
updateReferences, solution, document, field, finalFieldName, generatedPropertyName, cancellationToken).ConfigureAwait(false);
document = solutionNeedingProperty.GetDocument(document.Id);
var markFieldPrivate = field.DeclaredAccessibility != Accessibility.Private;
var rewrittenFieldDeclaration = await RewriteFieldNameAndAccessibility(finalFieldName, markFieldPrivate, document, declarationAnnotation, cancellationToken).ConfigureAwait(false);
document = await Formatter.FormatAsync(document.WithSyntaxRoot(rewrittenFieldDeclaration), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
var linkedDocument = solution.GetDocument(linkedDocumentId);
var updatedLinkedRoot = await RewriteFieldNameAndAccessibility(finalFieldName, markFieldPrivate, linkedDocument, declarationAnnotation, cancellationToken).ConfigureAwait(false);
var updatedLinkedDocument = await Formatter.FormatAsync(linkedDocument.WithSyntaxRoot(updatedLinkedRoot), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
solution = updatedLinkedDocument.Project.Solution;
}
document = solution.GetDocument(document.Id);
semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
compilation = semanticModel.Compilation;
var newRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var newDeclaration = newRoot.GetAnnotatedNodes<SyntaxNode>(declarationAnnotation).First();
field = semanticModel.GetDeclaredSymbol(newDeclaration, cancellationToken) as IFieldSymbol;
var generatedProperty = GenerateProperty(generatedPropertyName, finalFieldName, originalField.DeclaredAccessibility, originalField, field.ContainingType, new SyntaxAnnotation(), document, cancellationToken);
var codeGenerationService = document.GetLanguageService<ICodeGenerationService>();
var solutionWithProperty = await AddPropertyAsync(document, document.Project.Solution, field, generatedProperty, cancellationToken).ConfigureAwait(false);
return new Result(solutionWithProperty, originalField.ToDisplayString(), originalField.GetGlyph());
}
示例2: UpdateReferencesAsync
private async Task<Solution> UpdateReferencesAsync(
bool updateReferences, Solution solution, Document document, IFieldSymbol field, string finalFieldName, string generatedPropertyName, CancellationToken cancellationToken)
{
if (!updateReferences)
{
return solution;
}
if (field.IsReadOnly)
{
// Inside the constructor we want to rename references the field to the final field name.
var constructorSyntaxes = GetConstructorNodes(field.ContainingType).ToSet();
if (finalFieldName != field.Name && constructorSyntaxes.Count > 0)
{
solution = await Renamer.RenameSymbolAsync(solution, field, finalFieldName, solution.Workspace.Options,
location => constructorSyntaxes.Any(c => c.Span.IntersectsWith(location.SourceSpan)), cancellationToken: cancellationToken).ConfigureAwait(false);
document = solution.GetDocument(document.Id);
var compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
field = field.GetSymbolKey().Resolve(compilation, cancellationToken: cancellationToken).Symbol as IFieldSymbol;
}
// Outside the constructor we want to rename references to the field to final property name.
return await Renamer.RenameSymbolAsync(solution, field, generatedPropertyName, solution.Workspace.Options,
location => !constructorSyntaxes.Any(c => c.Span.IntersectsWith(location.SourceSpan)), cancellationToken: cancellationToken).ConfigureAwait(false);
}
else
{
// Just rename everything.
return await Renamer.RenameSymbolAsync(solution, field, generatedPropertyName, solution.Workspace.Options, cancellationToken).ConfigureAwait(false);
}
}
示例3: EncapsulateFieldAsync
private async Task<Result> EncapsulateFieldAsync(IFieldSymbol field, Document document, bool updateReferences, CancellationToken cancellationToken)
{
var originalField = field;
var finalNames = GeneratePropertyAndFieldNames(field);
var finalFieldName = finalNames.Item1;
var generatedPropertyName = finalNames.Item2;
// Annotate the field declarations so we can find it after rename.
var fieldDeclaration = field.DeclaringSyntaxReferences.First();
var declarationAnnotation = new SyntaxAnnotation();
document = document.WithSyntaxRoot(fieldDeclaration.SyntaxTree.GetRoot(cancellationToken).ReplaceNode(fieldDeclaration.GetSyntax(cancellationToken),
fieldDeclaration.GetSyntax(cancellationToken).WithAdditionalAnnotations(declarationAnnotation)));
var solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
var linkedDocument = solution.GetDocument(linkedDocumentId);
var linkedRoot = await linkedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var linkedFieldNode = linkedRoot.FindNode(fieldDeclaration.Span);
if (linkedFieldNode.Span != fieldDeclaration.Span)
{
continue;
}
var updatedRoot = linkedRoot.ReplaceNode(linkedFieldNode, linkedFieldNode.WithAdditionalAnnotations(declarationAnnotation));
solution = solution.WithDocumentSyntaxRoot(linkedDocumentId, updatedRoot);
}
document = solution.GetDocument(document.Id);
// Resolve the annotated symbol and prepare for rename.
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var compilation = semanticModel.Compilation;
field = field.GetSymbolKey().Resolve(compilation, cancellationToken: cancellationToken).Symbol as IFieldSymbol;
Solution solutionNeedingProperty = null;
// We couldn't resolve field after annotating its declaration. Bail
if (field == null)
{
return null;
}
if (updateReferences)
{
var locationsToIgnore = SpecializedCollections.EmptySet<TextSpan>();
var optionSet = document.Project.Solution.Workspace.Options;
if (field.IsReadOnly)
{
var locationSet = await RenameLocationSet.FindAsync(field, document.Project.Solution, optionSet, cancellationToken).ConfigureAwait(false);
var constructorSyntaxes = GetConstructorNodes(field.ContainingType);
var locations = locationSet.Locations.Where(l => constructorSyntaxes.Any(c => c.Span.IntersectsWith(l.Location.SourceSpan)));
if (locations.Any())
{
locationsToIgnore = locations.Select(l => l.Location.SourceSpan).ToSet();
locationSet = new RenameLocationSet(locations.ToSet(), field, document.Project.Solution, locationSet.ReferencedSymbols, locationSet.ImplicitLocations);
var resolution = await ConflictResolver.ResolveConflictsAsync(locationSet, field.Name, finalFieldName, optionSet, cancellationToken).ConfigureAwait(false);
document = resolution.NewSolution.GetDocument(document.Id);
semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
compilation = semanticModel.Compilation;
field = field.GetSymbolKey().Resolve(compilation, cancellationToken: cancellationToken).Symbol as IFieldSymbol;
}
}
var renameLocationSet = await RenameLocationSet.FindAsync(field, document.Project.Solution, optionSet, cancellationToken).ConfigureAwait(false);
renameLocationSet = new RenameLocationSet(renameLocationSet.Locations.Where(l => !locationsToIgnore.Contains(l.Location.SourceSpan)).ToSet(),
renameLocationSet.Symbol, renameLocationSet.Solution, renameLocationSet.ReferencedSymbols, renameLocationSet.ImplicitLocations);
if (renameLocationSet.Locations.Any() || renameLocationSet.ImplicitLocations.Any())
{
var conflictResolution = await ConflictResolver.ResolveConflictsAsync(renameLocationSet, field.Name, generatedPropertyName, optionSet, cancellationToken).ConfigureAwait(false);
if (!conflictResolution.ReplacementTextValid)
{
return null;
}
solutionNeedingProperty = conflictResolution.NewSolution;
document = solutionNeedingProperty.GetDocument(document.Id);
}
}
else
{
solutionNeedingProperty = document.Project.Solution;
document = solutionNeedingProperty.GetDocument(document.Id);
}
var markFieldPrivate = field.DeclaredAccessibility != Accessibility.Private;
var rewrittenFieldDeclaration = await RewriteFieldNameAndAccessibility(finalFieldName, markFieldPrivate, document, declarationAnnotation, cancellationToken).ConfigureAwait(false);
document = await Formatter.FormatAsync(document.WithSyntaxRoot(rewrittenFieldDeclaration), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
//.........这里部分代码省略.........