public CodeActionEdit GetEdit(CancellationToken cancellationToken) { var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken); var semanticModel = (SemanticModel)document.GetSemanticModel(cancellationToken); // Retrieves the get accessor declarations of the specified property. var getter = property.AccessorList.Accessors.FirstOrDefault(ad => ad.Kind == SyntaxKind.GetAccessorDeclaration); // Retrieves the type that contains the specified property var containingType = semanticModel.GetDeclaredSymbol(property).ContainingType; // Find the backing field of the property var backingField = GetBackingField(getter, containingType); // Rewrite property var propertyRewriter = new PropertyRewriter(semanticModel, backingField, property); var newRoot = propertyRewriter.Visit(tree.GetRoot(cancellationToken)); return(new CodeActionEdit(document.UpdateSyntaxRoot(newRoot))); }
private async Task<Document> ConvertToAutoPropertyAsync(Document document, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var tree = (SyntaxTree)await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); var semanticModel = (SemanticModel)await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); // Retrieves the get accessor declarations of the specified property. var getter = property.AccessorList.Accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.GetAccessorDeclaration); // Retrieves the type that contains the specified property var containingType = semanticModel.GetDeclaredSymbol(property).ContainingType; // Find the backing field of the property var backingField = await GetBackingFieldAsync(document, getter, containingType, cancellationToken).ConfigureAwait(false); // Rewrite property var propertyRewriter = new PropertyRewriter(semanticModel, backingField, property); var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false); var newRoot = propertyRewriter.Visit(root); return document.WithSyntaxRoot(newRoot); }
private async Task <Document> ConvertToAutoPropertyAsync(Document document, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var tree = (SyntaxTree)await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); var semanticModel = (SemanticModel)await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); // Retrieves the get accessor declarations of the specified property. var getter = property.AccessorList.Accessors.FirstOrDefault(ad => ad.CSharpKind() == SyntaxKind.GetAccessorDeclaration); // Retrieves the type that contains the specified property var containingType = semanticModel.GetDeclaredSymbol(property).ContainingType; // Find the backing field of the property var backingField = await GetBackingFieldAsync(document, getter, containingType, cancellationToken).ConfigureAwait(false); // Rewrite property var propertyRewriter = new PropertyRewriter(semanticModel, backingField, property); var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false); var newRoot = propertyRewriter.Visit(root); return(document.WithSyntaxRoot(newRoot)); }
public CodeActionEdit GetEdit(CancellationToken cancellationToken) { var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken); var semanticModel = (SemanticModel)document.GetSemanticModel(cancellationToken); // Retrieves the get accessor declarations of the specified property. var getter = property.AccessorList.Accessors.FirstOrDefault(ad => ad.Kind == SyntaxKind.GetAccessorDeclaration); // Retrieves the type that contains the specified property var containingType = semanticModel.GetDeclaredSymbol(property).ContainingType; // Find the backing field of the property var backingField = GetBackingField(getter, containingType); // Rewrite property var propertyRewriter = new PropertyRewriter(semanticModel, backingField, property); var newRoot = propertyRewriter.Visit(tree.GetRoot(cancellationToken)); return new CodeActionEdit(document.UpdateSyntaxRoot(newRoot)); }