简体   繁体   English

如何在 Visual Studio IDE 中使所有 IDisposable 类的颜色不同?

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

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.我希望实现 IDisposable 的类以特定颜色显示,以便我知道是否应该将它们包装在 using 块中。 Is there a setting or a process by which you can extend the IDE?是否有可以扩展 IDE 的设置或过程?

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.我认为一旦 Roslyn 出现,这将变得更容易/无扩展,但目前这并不容易,因为您无法轻松地从扩展中以 C# 形式访问代码。

In Resharper it's easy, though!不过,在 Resharper 中这很容易! My example was tested in ReSharper 9.0.我的示例在 ReSharper 9.0 中进行了测试。 Sadly, there's no easy way to give this to you.可悲的是,没有简单的方法可以将其提供给您。

  • Extensions -> Resharper -> Options -> Code Inspection -> Custom Patterns -> Add, dialog popup扩展 -> Resharper -> 选项 -> 代码检查 -> 自定义模式 -> 添加,弹出对话框
  • Select C# (upper left)选择 C#(左上角)
  • Select "Find" (upper right)选择“查找”(右上角)
  • Add the pattern of new $disp$($args$)添加new $disp$($args$)
  • Pattern severity: Show as suggestion模式严重性:显示为建议
  • Description: Disposable construction描述:一次性结构
  • "Add Placeholder" of type: Type , name: disp , type: System.IDisposable “添加占位符”类型: Type ,名称: disp ,类型: System.IDisposable
  • "Add Placeholder" of type: Arguments , name: args “添加占位符”类型: Arguments ,名称: args

Save and you'll now get a "suggestion" whenever a new disposable is being constructed.保存,现在每当构建新的一次性用品时,您都会收到“建议”。

Adding the pattern $disp$ $var$ = $exp$;添加模式$disp$ $var$ = $exp$; could also be helpful.也可能有帮助。

  • "Add Placeholder" of type: Type , name: disp , type: System.IDisposable “添加占位符”类型: Type ,名称: disp ,类型: System.IDisposable
  • "Add Placeholder" of type: Expression , name: exp “添加占位符”类型: Expression ,名称: exp
  • "Add Placeholder" of type: Identifier , name: var “添加占位符”类型: Identifier ,名称: var

在此处输入图片说明

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.您需要编写一个 Visual Studio 插件来完成此操作。

Visit http://msdn.microsoft.com/en-us/vsx/bb980955.aspx to get started.访问http://msdn.microsoft.com/en-us/vsx/bb980955.aspx以开始使用。 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这是一个链接,可以指向您正在寻找的内容: http : //msdn.microsoft.com/en-us/library/bb166778.aspx

You cannot.你不能。 This would require language service support and neither C# or VB.Net provide this functionality.这将需要语言服务支持,而 C# 或 VB.Net 均不提供此功能。

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.然而,这是一项非常重要的任务。

I am not sure, if FXCop or StyleCop can do this already.我不确定 FXCop 或 StyleCop 是否已经可以做到这一点。 But then, it will be a post-compile suggestion/warning.但是,这将是一个编译后建议/警告。

Resharper suggests this, I guess.我想,Resharper 建议这样做。

当然,有大量工具可用于构建 VS 扩展, 请参阅 Visual Studio 2008 SDK 1.1但是构建此类加载项所需的时间将需要更多时间,您将花费更多时间浏览组件并确定它们是否是一次性的

The word on the street is this kind of thing will be much easier in VS.NET 2010 .街上的一句话是这种事情在 VS.NET 2010 中容易得多 The editor is being rewritten in WPF.编辑器正在用 WPF 重写。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得Visual Studio项目中使用的所有类和方法的列表? - How can I get a list of all classes and methods used in a Visual Studio project? 如何找到实现IDisposable的所有类? - How to find all Classes implementing IDisposable? 如何使Visual Studio 2010显示RadioButton的所有可用事件 - How can I make Visual Studio 2010 show all available events for RadioButton 使Visual Studio 2015成为Visual Basic IDE - Make Visual Studio 2015 a Visual Basic IDE 有没有一种方法可以在Visual Studio IDE中显示方法的注释参数? - Is there a way that I can show comment parameters for a method in the Visual Studio IDE? 如何在Visual Studio IDE C#中隐藏跟踪代码? - How can I hide tracing code in Visual Studio IDE C#? 如何为Visual Studio IDE的特定文件类型创建代码生成器? - How can I create a code generator for specific file types for the Visual Studio IDE? 我必须在所有类上实现IDisposable,还是基类足够? - Must I implement IDisposable on all classes, or is a base class sufficient? 我如何为我的类实现IDisposable,以便它可以在'using'块中使用? - How would I implement IDisposable for my classes so that it can be used in a 'using' block? 如何在 C# (Visual Studio) 中进行热键控制 - How can I make Hotkey control at C# (Visual Studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM