Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper Code Pattern for IDisposable not inside using

I'd like to know how to build a Resharper (6.1) code pattern to search and replace the following issues:

var cmd = new SqlCommand();
cmd.ExecuteNonQuery();

and turn it into this:

using (var cmd = new SqlCommand())
{
  cmd.ExecuteNotQuery();
}

and:

 StreamReader reader = new StreamReader("myfile.txt");
 string line = reader.Read();
 Console.WriteLine(line);

becomes:

using (StreamReader reader = new StreamReader("file.txt"))
{
   string line = reader.ReadLine();
   Console.WriteLine(line);
}

EDIT: Thanks for the answers, but I'm looking for anything that implements IDisposable

like image 917
Brett Veenstra Avatar asked Jul 12 '26 16:07

Brett Veenstra


1 Answers

Search pattern:

var $cmd$ = $sqlcommand$;
$cmd$.ExecuteNonQuery();

Replace pattern:

using (var $cmd$ = $sqlcommand$)
{
$cmd$.ExecuteNonQuery();
}

where cmd = identifier

and sqlcommand = expression of type System.Data.SqlClient.SqlCommand

like image 143
Jura Gorohovsky Avatar answered Jul 17 '26 07:07

Jura Gorohovsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!