C#编译器选项,用于查找实现IDisposable的类 [英] C# compiler option to find classes that implement IDisposable

查看:69
本文介绍了C#编译器选项,用于查找实现IDisposable的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对C#来说很新,我可能误解了这一点。如果是这样,请承担

我!


据我所知,任何实现

的类的实例IDisposable接口必须调用Dispose方法才能创建

资源的漏洞!?这可以通过使用显式调用Dispose或

来实现。声明。


例如,一个递归方法,可以创建数百或数千个
实例,例如OleDbConnection,OleDbCommand和

OleDbDataReader,最终会造成破坏,除非他们明确地通过Dispose方法或using语句处理了
,对吗?


现在,如果全部以上是真的,识别所有

类实现IDisposable接口的最简单方法是什么? 自动列表成员

弹出窗口我VS将显示Dispose方法(如果它存在),但是我必须始终保持警惕,并且,我不是!有没有可能让C#

编译器在使用时生成关于这些类的警告消息?


最好的问候Carl Johansson

Being quite new to C#, I may have misunderstood this. If so please bear with
me!

As far as I can understand, any instances of a class that implements the
IDisposable interface must call the Dispose method not create leaks of
resources!? This can be accomplished by explicitly calling Dispose or
through the "using" statement.

For example, a recursive method that creates hundreds or thousands of
instances of, for example, OleDbConnection, OleDbCommand, and
OleDbDataReader, would eventually cause havoc unless they were explicitly
disposed through the Dispose method or the using statement, right?

Now, if all of the above is true, what is the easiest way to identify all
classes the implement the IDisposable interface? The "Auto list members"
pop-up window i VS will show the Dispose method if it is present, but then I
must always be alert, and that, I am not! Is it possible to get the C#
compiler to generate warning messages about these classes when used?

Best Regards Carl Johansson

推荐答案

9月24日上午11点32分,Carl Johansson

< carl.johans ... @ nogarbagehallde.comwrote:
On Sep 24, 11:32 am, "Carl Johansson"
<carl.johans...@nogarbagehallde.comwrote:

作为C#的新手,我可能误解了这一点。如果是这样,请承担

我!


据我所知,任何实现

的类的实例IDisposable接口必须调用Dispose方法才能创建

资源的漏洞!?这可以通过使用显式调用Dispose或

来实现。声明。
Being quite new to C#, I may have misunderstood this. If so please bear with
me!

As far as I can understand, any instances of a class that implements the
IDisposable interface must call the Dispose method not create leaks of
resources!? This can be accomplished by explicitly calling Dispose or
through the "using" statement.



不,实例不会调用Dispose - 这些类的*客户*

调用Dispose。

这并不意味着它总是被切断和干燥的地方应该处理掉b
。例如,如果您创建一个Stream并将其传递到

Image.FromStream,则需要保持流打开 - 图像处理时图像将关闭


No, the instances don''t call Dispose - the *clients* of those classes
call Dispose.
That doesn''t mean it''s always cut-and-dried as to where things should
be disposed. For instance, if you create a Stream and pass it into
Image.FromStream, you need to leave the stream open - image will close
it when the image is disposed.


例如,一个递归方法,可以创建数百或数千个
实例,例如OleDbConnection,OleDbCommand和

OleDbDataReader,最终会造成破坏,除非他们明确地通过Dispose方法或using语句处理了
,对吗?
For example, a recursive method that creates hundreds or thousands of
instances of, for example, OleDbConnection, OleDbCommand, and
OleDbDataReader, would eventually cause havoc unless they were explicitly
disposed through the Dispose method or the using statement, right?



是。

Yes.


现在,如果以上所有都是真的,最简单的方法是什么?识别所有

类实现IDisposable接口? 自动列表成员

弹出窗口我VS将显示Dispose方法(如果它存在),但是我必须始终保持警惕,并且,我不是!是否可以使用C#

编译器在使用时生成有关这些类的警告消息?
Now, if all of the above is true, what is the easiest way to identify all
classes the implement the IDisposable interface? The "Auto list members"
pop-up window i VS will show the Dispose method if it is present, but then I
must always be alert, and that, I am not! Is it possible to get the C#
compiler to generate warning messages about these classes when used?



否 - 因为你并不总是想要在你创造它的同一个地方处理某些东西。


我担心你基本上必须遵守纪律。


Jon

No - because you don''t always want to dispose of something in the same
place you create it.

I''m afraid you basically have to be disciplined about it.

Jon

Carl,


当收集内存时,对象会自行处理。

没有什么棘手的......它是设计模式的一部分。对于

OleDbConnection,一个祖先已经


~Component()

{

this.Dispose (假);

}


和Dispose是虚拟的。


你应该在IDisposables上调用Dispose完成是因为

在内存收集器运行之前可能需要一段时间,但忘记

不会导致havoc。


-James

Carl,

The objects dispose themselves when they are memory collected.
Nothing tricky... its part of the design pattern. For
OleDbConnection, an ancestor has

~Component()
{
this.Dispose(false);
}

and Dispose is virtual.

You should call Dispose on IDisposables when you are finished because
it might be a while before the memory collector runs, but forgetting
will not cause "havoc".

-James


9月24日下午1:39,james< james.w ... @ gmail.comwrote:
On Sep 24, 1:39 pm, james <james.w...@gmail.comwrote:

对象在收集内存时自行处理。
The objects dispose themselves when they are memory collected.



但是,你几乎*从不*依赖于此。如果

实现IDisposable,你应该在完成后调用Dispose

吧。


终结器仅仅是腰带和括号以防万一你忘了明确处理东西。


< snip>

You should almost *never* rely on this, however. If something
implements IDisposable, you should call Dispose when you''re done with
it.

The finalizer is merely a "belt and braces" approach in case you
forget to explicitly dispose things.

<snip>


完成后你应该在IDisposables上调用Dispose,因为在内存收集器运行之前它可能需要一段时间,但是忘记

不会导致havoc。
You should call Dispose on IDisposables when you are finished because
it might be a while before the memory collector runs, but forgetting
will not cause "havoc".



嗯,它可以做到。如果您忘记明确关闭连接,那么
可能会不必要地耗尽连接。同样不是

处理FileStream可能会阻止你重新打开文件

进行写作。

这也是不确定的 - 所以复制可靠的这个问题

几乎是不可能的。


就个人而言,我认为是破坏...


Jon

Um, it could do. If you forget to close connections explicitly, you
could end up running out of connections unnecessarily. Likewise not
disposing of a FileStream could stop you from then reopening the file
for writing.
This is also non-deterministic - so reproducing this issue reliably
could almost impossible.

Personally, I count that as "havoc"...

Jon


这篇关于C#编译器选项,用于查找实现IDisposable的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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