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

删除回忆录丶 提交于 2019-12-10 01:23:55

问题


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?


回答1:


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




回答2:


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.

  • 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. All things equal with exp is defined as an expression.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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();
      }
    }
  }
}


来源:https://stackoverflow.com/questions/1189024/how-can-i-make-all-of-the-idisposable-classes-colored-differently-in-the-visual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!