Мне нужна была та же функциональность, и я не смог ее найти. Я подумывал написать надстройку для ReSharper с использованием Api, но вместо этого выбрал обычный макрос Visual Studio. Этот макрос переименовывает методы и личные поля в текущем документе в настройки ReSharper по умолчанию, но его можно легко изменить, чтобы перебирать все файлы в проекте или решении.
Сохраните этот код в виде файла .vb и импортируйте его в свои макросы VS.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module RenameMembers
Enum NamingStyle
UpperCamelCase
LowerCamelCase
End Enum
Public Sub RenameMembers()
Try
'Iterate through all code elements in the open document
IterateCodeElements(ActiveDocument.ProjectItem.FileCodeModel.CodeElements)
Catch ex As System.Exception
End Try
End Sub
'Iterate through all the code elements in the provided element
Private Sub IterateCodeElements(ByVal colCodeElements As CodeElements)
Dim objCodeElement As EnvDTE.CodeElement
If Not (colCodeElements Is Nothing) Then
For Each objCodeElement In colCodeElements
Try
Dim element As CodeElement2 = CType(objCodeElement, CodeElement2)
If element.Kind = vsCMElement.vsCMElementVariable Then
RenameField(element)
ElseIf element.Kind = vsCMElement.vsCMElementFunction Then
'Rename the methods
ApplyNamingStyle(element, NamingStyle.UpperCamelCase)
ElseIf TypeOf objCodeElement Is EnvDTE.CodeNamespace Then
Dim objCodeNamespace = CType(objCodeElement, EnvDTE.CodeNamespace)
IterateCodeElements(objCodeNamespace.Members)
ElseIf TypeOf objCodeElement Is EnvDTE.CodeClass Then
Dim objCodeClass = CType(objCodeElement, EnvDTE.CodeClass)
IterateCodeElements(objCodeClass.Members)
End If
Catch
End Try
Next
End If
End Sub
'Rename the field members according to our code specifications
Private Sub RenameField(ByRef element As CodeElement2)
If element.Kind = vsCMElement.vsCMElementVariable Then
Dim field As EnvDTE.CodeVariable = CType(element, EnvDTE.CodeVariable)
If (field.Access = vsCMAccess.vsCMAccessPrivate) Then
'private static readonly
If (field.IsShared AndAlso field.IsConstant) Then
ApplyNamingStyle(element, NamingStyle.UpperCamelCase)
ElseIf (Not field.IsShared) Then
'private field (readonly but not static)
ApplyNamingStyle(element, NamingStyle.LowerCamelCase, "_")
Else
ApplyNamingStyle(element, NamingStyle.UpperCamelCase)
End If
Else
'if is public, the first letter should be made uppercase
ToUpperCamelCase(element)
End If
'if public or protected field, start with uppercase
End If
End Sub
Private Function ApplyNamingStyle(ByRef element As CodeElement2, ByVal style As NamingStyle, Optional ByVal prefix As String = "", Optional ByVal suffix As String = "")
Dim the_string As String = element.Name
If (Not the_string Is Nothing AndAlso the_string.Length > 2) Then
If (style = NamingStyle.LowerCamelCase) Then
ToLowerCamelCase(the_string)
ElseIf (style = NamingStyle.UpperCamelCase) Then
ToUpperCamelCase(the_string)
Else
'add additional styles here
End If
End If
AddPrefixOrSuffix(the_string, prefix, suffix)
If (Not element.Name.Equals(the_string)) Then
element.RenameSymbol(the_string)
End If
End Function
Private Function ToLowerCamelCase(ByRef the_string As String)
the_string = the_string.Substring(0, 1).ToLower() & the_string.Substring(1)
End Function
Private Function AddPrefixOrSuffix(ByRef the_string As String, Optional ByVal prefix As String = "", Optional ByVal suffix As String = "")
If (Not the_string.StartsWith(prefix)) Then
the_string = prefix + the_string
End If
If (Not the_string.EndsWith(suffix)) Then
the_string = the_string + suffix
End If
End Function
Private Function ToUpperCamelCase(ByRef the_string As String)
the_string = the_string.Substring(0, 1).ToUpper() & the_string.Substring(1)
End Function
End Module