Title covers it all. I'd like classes which implement IDisposable to show up in a specific color so I can know if I should wrap them in a using block. Is there a setting or a process by which you can extend the IDE?
7 Answers
I assume this will become easier/extension-free once Roslyn comes out, but this is presently not easy because you can't access the code as C# from an extension easily.
In Resharper it's easy, though! My example was tested in ReSharper 9.0. Sadly, there's no easy way to give this to you.
- Extensions -> Resharper -> Options -> Code Inspection -> Custom Patterns -> Add, dialog popup
- Select C# (upper left)
- Select "Find" (upper right)
- Add the pattern of
new $disp$($args$)
- Pattern severity: Show as suggestion
- Description: Disposable construction
- "Add Placeholder" of type:
Type
, name:disp
, type:System.IDisposable
- "Add Placeholder" of type:
Arguments
, name:args
Save and you'll now get a "suggestion" whenever a new disposable is being constructed.
Adding the pattern $disp$ $var$ = $exp$;
could also be helpful.
- "Add Placeholder" of type:
Type
, name:disp
, type:System.IDisposable
- "Add Placeholder" of type:
Expression
, name:exp
- "Add Placeholder" of type:
Identifier
, name:var

- 3,418
- 4
- 30
- 51

- 3,202
- 1
- 27
- 25
-
1Good workaround, but not as good as using a special color for variables/expressions of type IDisposable since it sort of makes it look like all cases of creating disposables are errors. R# feature request submitted: https://youtrack.jetbrains.com/issue/RSRP-472248 – dlf Nov 16 '18 at 14:15
-
can you please mention how to setup "Add Placeholder" parts in case of your last mentioned pattern (`$disp$ $var$ = $exp$;`) so we could just see all declarations of disposable items in .cs file. – T.Todua Jun 03 '20 at 12:34
-
Thank. just i doubt that in that addition, you described `$args$` while missed to mention about `$var$` ? sorry for bothering, just I'm new there. – T.Todua Jun 04 '20 at 07:07
-
@T.Todua I did miss that. Thanks! Fixed now. – Joseph Lennox Jun 05 '20 at 20:20
It is certainly possible to do this though it isn't as simple as just changing a setting. You would need to write a Visual Studio addin to accomplish this.
Visit http://msdn.microsoft.com/en-us/vsx/bb980955.aspx to get started. As others will point out. This is not for the faint of heart.
Here's a link that may point you toward what you are looking for:http://msdn.microsoft.com/en-us/library/bb166778.aspx

- 11,954
- 3
- 52
- 117
-
good points. here is another good link http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.textmanager.interop.ivshicoloritem.aspx – Stan R. Jul 27 '09 at 16:17
-
1
You cannot. This would require language service support and neither C# or VB.Net provide this functionality.
Cannot is probably too strong of a word. It's certainly possible to do this with an Add-In which does deep inspection of the code and figures out hierarchies. However it's a very non-trivial task.

- 733,204
- 149
- 1,241
- 1,454
I am not sure, if FXCop or StyleCop can do this already. But then, it will be a post-compile suggestion/warning.
Resharper suggests this, I guess.

- 33,172
- 3
- 63
- 88
Sure, there is a large set of tools to build VS extensions, see Visual Studio 2008 SDK 1.1 But time required to build such an add-in will require more time that you will spend by browsing components and determining whether they are Disposable or not

- 3,328
- 4
- 23
- 39
The word on the street is this kind of thing will be much easier in VS.NET 2010. The editor is being rewritten in WPF.

- 27,069
- 12
- 75
- 95
Maybe I'm a bad person for doing this, but I've been using this piece of code recently:
public static void BulkDispose(object[] objects)
{
foreach (object o in objects)
{
if (o != null)
{
if (o is IDisposable)
{
IDisposable disposable = o as IDisposable;
disposable.Dispose();
}
}
}
}

- 6,119
- 3
- 29
- 47
-
It's not uncommon for Dispose to throw exceptions ("already disposed"), so you'd want each dispose call wrapped in a try/catch. – Joseph Lennox Jan 28 '15 at 21:47
-
1Dispose shoud *never* throw because of multiple calls. If it does, the component in question doesn't comply with the IDisposable contract. See https://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx under Remarks: "The object must not throw an exception if its Dispose method is called multiple times." – bernhof Sep 15 '17 at 07:16