private static async Task RenameFieldAccordingToPropertyNameAsync( RefactoringContext context, IdentifierNameSyntax identifierName) { if (!identifierName.IsQualified() || identifierName.IsQualifiedWithThis()) { PropertyDeclarationSyntax propertyDeclaration = identifierName.FirstAncestor <PropertyDeclarationSyntax>(); if (propertyDeclaration != null) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); var fieldSymbol = semanticModel .GetSymbolInfo(identifierName, context.CancellationToken) .Symbol as IFieldSymbol; if (fieldSymbol?.IsPrivate() == true) { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (propertySymbol != null && fieldSymbol.IsStatic == propertySymbol.IsStatic && object.Equals(fieldSymbol.ContainingType, propertySymbol.ContainingType)) { string newName = TextUtility.ToCamelCase(propertySymbol.Name, context.Settings.PrefixFieldIdentifierWithUnderscore); if (!string.Equals(newName, fieldSymbol.Name, StringComparison.Ordinal)) { string fieldName = identifierName.Identifier.ValueText; context.RegisterRefactoring( $"Rename field to '{newName}'", cancellationToken => { return(SymbolRenamer.RenameAsync( context.Document, fieldSymbol, newName, cancellationToken)); }); } } } } } }
private static void RenameFieldAccordingToPropertyName( CodeRefactoringContext context, IdentifierNameSyntax identifierName, SemanticModel semanticModel) { if (!identifierName.IsQualified() || identifierName.IsQualifiedWithThis()) { PropertyDeclarationSyntax propertyDeclaration = identifierName.FirstAncestor <PropertyDeclarationSyntax>(); if (propertyDeclaration != null) { var fieldSymbol = semanticModel .GetSymbolInfo(identifierName, context.CancellationToken) .Symbol as IFieldSymbol; if (fieldSymbol?.DeclaredAccessibility == Accessibility.Private) { IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (propertySymbol != null && fieldSymbol.IsStatic == propertySymbol.IsStatic && object.Equals(fieldSymbol.ContainingType, propertySymbol.ContainingType)) { string newName = NamingHelper.ToCamelCaseWithUnderscore(propertySymbol.Name); if (!string.Equals(newName, fieldSymbol.Name, StringComparison.Ordinal)) { context.RegisterRefactoring( $"Rename field to '{newName}'", cancellationToken => { return(fieldSymbol.RenameAsync( newName, context.Document, cancellationToken)); }); } } } } } }