How can I make all of the IDisposable classes colored differently in the Visual Studio IDE?

前端 未结 7 1361
小蘑菇
小蘑菇 2021-01-07 21:47

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

相关标签:
7条回答
  • 2021-01-07 21:58

    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

    0 讨论(0)
  • 2021-01-07 22:01

    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();
          }
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-07 22:02

    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.

    0 讨论(0)
  • 2021-01-07 22:06

    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

    0 讨论(0)
  • 2021-01-07 22:16

    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.

    0 讨论(0)
  • 2021-01-07 22:17

    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 popsup
    • 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

    enter image description here

    0 讨论(0)
提交回复
热议问题