[英]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.可悲的是,没有简单的方法可以将其提供给您。
new $disp$($args$)
添加new $disp$($args$)
Type
, name: disp
, type: System.IDisposable
“添加占位符”类型: Type
,名称: disp
,类型: System.IDisposable
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.也可能有帮助。
Type
, name: disp
, type: System.IDisposable
“添加占位符”类型: Type
,名称: disp
,类型: System.IDisposable
Expression
, name: exp
“添加占位符”类型: Expression
,名称: exp
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.