Creating custom refactoring for Visual Studio

Is there a way to write my own refactorings or code transformations for Visual Studio?

Example: I have a codebase with a billion instances:

DbConnection conn = null;
conn = new DbConnection();
conn.Open();
...a number of statements using conn...
conn.Close();
conn = null;

I would like to convert this to:

using (DbConnection conn = GetConnection()){
   ...statements...
}

The above template appears everywhere.

Edit: the above example. The thing is, I need to make a series of code transformations that are too complex to perform with text search-replace. I wonder if I can connect to the same mechanism underlying the built-in refactoring to write my own code transformations.

+5
source share
3 answers

, "", . ReSharper - , , . !

, , one

+2

, , , ( , Dispose()). , "Resharper", "Refactor! Pro" " " ( ). "Refactor! Pro" ( , ), ( , DbConnection conn = new SqlConnection();), ( , ). :

  • Resharper ( )
  • ,
+1

You will need to write a macro for this.

0
source

All Articles