找到所有的类,我忘了打电话给我安排 [英] Finding all the classes , where i have forgotten to call dispose on

查看:133
本文介绍了找到所有的类,我忘了打电话给我安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他,所以我正在写很多代码.
现在我想知道我是否已写完所有处置文件.

例如:

He , so i am writing a lot of code.
And now i wanna know if i have written all the dispose''s.

Like for example :

private void DoDadaBoom()
{
   var bitmap = new bitmap(params)
   DoStuff(bitmap)
   dode = new bitmap.clone();
   bitmap.dispose()
}



但是:
也许我可能忘了写'' bitmap.dispose()''.

我想欺骗以获得良好的概述>对于每个被遗忘的处置呼叫.

有任何想法吗?



But :
Maybe i could have forgotten to write ''bitmap.dispose()''.

I wanna trick to get a good overview > for every potentionally forgotten dispose call.

Any ideas ?

推荐答案

我们网站上的以下文章对您有用.

查找未处理的对象 [
Following article of our site will be useful to you.

Finding Undisposed Objects[^]

Hope this will help!


我认为您不应该担心未处理的物体.那是GarbageCollector的工作.如果对象没有对它的引用,则垃圾收集器会自动将其处置.

但是,如果您只想调用dispose方法,则每当使用IDisposable对象时,请使用using关键字将其包围:

I think you should not worry about undisposed objects. That is the GarbageCollector''s job. If an object has no references to it then the garbage colector automatically disposes it.

But if you just want to call the dispose method, whenever you use an IDisposable object, suround it with the using keyword:

private void DoDadaBoom(){   
   using(var bitmap = new bitmap(params))
   {   
        DoStuff(bitmap);
        dode = new bitmap.clone();
   }
}



using关键字将对象放置在using块的末尾.



The using keyword disposes the object at the end of the using block.


贾尔诺,

尝试Jinal Desai提供的解决方案.这将帮助您在项目中找到一次性对象.

最后,我的一个建议,即不要调用dispose方法,而是使用using(....){}包围对象的实例,如下所示:

假设您有一个从IDisposable接口继承的可抛弃对象.像这样使用类的实例:

using(Person p = new Person())
{
.
.
.
}

在这里,您不必显式调用Dispose()方法.运行时将使用它.希望这些信息对您有所帮助.

记住:如果有帮助,请标记为答案".

问候,
库纳尔(Kunal)
Hi Jarno,

Try the solution given by Jinal Desai. It will help you to find the disposable object in your project.

Lastly, one suggestion from me i.e. don''t call the dispose method instead surround the instance of the object with using(....) {} like below:

Suppose, you have a disposable object which inherited from IDisposable interface. Work with the instance of the class like this:

using(Person p = new Person())
{
.
.
.
}

Here, you don''t have to call the Dispose() method explicitly. The runtime will take of it. Hope this information will help you.

Remember: Please "Mark As Answer" if this helps you.

Regards,
Kunal


这篇关于找到所有的类,我忘了打电话给我安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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