As a (silly?) alternative to shelling out for Resharper and its ilk, Visual Studio does have the concept of External Tools (in the Tools menu), which you could (ab)use to do something like:
- Title:
Is Disposa&ble
- Command:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- Arguments:
-Command "&{$i=[Type]::GetType('System.IDisposable');[AppDomain]::CurrentDomain.GetAssemblies()|%{ $_.GetTypes()}|?{$_.FullName.EndsWith('.$(CurText)')}|%{New-Object PSObject -Property @{'Type'=$_;'IDisposable'=$i.IsAssignableFrom($_)}}|ft}"
- Use Output window: checked
This would read whatever string you had selected in the editor, search for .NET types with that string as a name and show a True/False as to whether that string implemented IDisposable.
The Powershell command in the tool is just the quickest approach I could do to demonstrate the possibility, but it is far from perfect -- it only finds types in assemblies that Powershell loads by default. If you wanted to expand on the idea, you could build a command-line .NET app that loaded your project and scanned all the assemblies that your project loaded.
If you highlighted the word Stream
in your code, for example, and ran your external tool (ALT+T,ALT+B in the example), it would return:
Type IDisposable
---- -----------
System.IO.Stream True
To break down the Powershell command:
&{ $i=[Type]::GetType('System.IDisposable'); # Get the IDisposable interface
[AppDomain]::CurrentDomain.GetAssemblies() ` # Get all loaded assemblies
| %{ $_.GetTypes() } ` # For each assembly, get all types
| ?{ $_.FullName.EndsWith('.$(CurText)') } ` # Filter types that are named $(CurText) - $(CurText) is a macro within VS External Tools
| %{ New-Object PSObject -Property @{ # For each type, return an object containing...
'Type' = $_; # ...the type name...
'IDisposable' = $i.IsAssignableFrom($_) # ...and whether the IDisposable interface is implemented
} } `
| ft } # Format all returned objects as a table
IDisposable
interface and use ausing
statement to instanciate those? – MatchDispose()
method on an object you create without implementingIDisposable
. – UpstretchedIDisposable
that you probably don't need to worry about disposing. blogs.msdn.microsoft.com/pfxteam/2012/03/25/… – Matthaus