ReSharper的自定义搜索模式,以警告IDisposable的对象 [英] Resharper custom search pattern to warn IDisposable objects

查看:165
本文介绍了ReSharper的自定义搜索模式,以警告IDisposable的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于ReSharper的仍然没有给出关于实施IDisposable的对象的任何警告,我想创造一些可自定义的搜索模式在ReSharper的5.0。

Since resharper still doesn't give out any warning regarding objects implementing IDisposable, I'd like to create some custom search patterns available in resharper 5.0.

到目前为止,我有这样的:

So far I have this:

(并且不介意我的模式取代意见,我真的不关心它,我只是想在与处置的对象打交道,在code一个明确的警告。)

(And don't mind my replace comments in the patterns, I don't really care about it, I just want a clear warning in the code when dealing with disposable objects.)

- <CustomPatterns>
- <Pattern Severity="WARNING">
  <Comment>This class implements IDisposable interface.</Comment> 
  <ReplaceComment>Please use Using statement, or dispose the object manually when done using.</ReplaceComment> 
  <SearchPattern>$type$</SearchPattern> 
  <Params /> 
- <Placeholders>
  <IdentifierPlaceholder Name="var" Type="" ExactType="False" RegEx="" CaseSensitive="True" /> 
  <TypePlaceholder Name="type" Type="System.IDisposable" ExactType="False" /> 
  <ArgumentPlaceholder Name="args" Minimal="-1" Maximal="-1" /> 
  </Placeholders>
  </Pattern>
- <Pattern Severity="WARNING">
  <Comment>This class implements IDisposable interface.</Comment> 
  <ReplaceComment>Please use Using statement, or dispose the object manually when done using.</ReplaceComment> 
  <SearchPattern>new $type$($args$)</SearchPattern> 
  <Params /> 
- <Placeholders>
  <IdentifierPlaceholder Name="var" Type="" ExactType="False" RegEx="" CaseSensitive="True" /> 
  <TypePlaceholder Name="type" Type="System.IDisposable" ExactType="False" /> 
  <ArgumentPlaceholder Name="args" Minimal="-1" Maximal="-1" /> 
  </Placeholders>
  </Pattern>
  </CustomPatterns>

该处理的变量声明的情况下,例如:

This handles cases of variable declaration, e.g.

Bitmap myBitmap = GetBitmap();
private Bitmap _bitmap;

和CTOR调用,例如。

and CTOR calls, e.g.

var myBitmap = new Bitmap(...);

它不支持什么,是这样的:

What it doesn't support, is this:

var myBitmap = GetBitmap();

我找不到如何定义要么找到无功的使用,或方法返回类型的搜索模式的任何例子,那就是typeof运算IDisposable的。

I can't find any example of how to define a search pattern that will either find 'var' usage, or a method return type, that is typeof IDisposable.

我敢肯定有办法,我找不到它,虽然。

I'm sure there's a way, I can't find it though.

推荐答案

这些模式的问题在于,它们没有消失,当你真正处理的对象,但可能是使用语句中的局部变量声明。它也没有跟踪对象的所有权,例如工厂方法和直通方式。所以我相信通过结构化模式使得它基本是没用的。

The problem with these patterns is that they don't go away when you actually dispose the object, except may be for local variable declarations inside using statements. It also doesn't track object ownership, e.g. for factory methods and pass-through methods. So I believe making it through structured patterns is next to useless.

无论如何,你可能需要两种模式的局部变量的检查如

Anyway, you may need two patterns for local variable checks like

var $identifier$ = $expression$; 
$type$ $identifier$ = $expression$;

,其中前pression和类型实现IDisposable接口。

where expression and type are implementing IDisposable.

这篇关于ReSharper的自定义搜索模式,以警告IDisposable的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆