C#ow处理需要调用Dispose方法的对象 [英] C# ow the disposing of an object that need to call the Dispose method works

查看:88
本文介绍了C#ow处理需要调用Dispose方法的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我的问题是如何处理需要调用Dispose方法的对象。顺便说一下,英语不是我的母语,如果有错误的话会很抱歉



我想获得系统中安装的服务列表为此,我使用WMI。首先,我使用GetInstance方法获取Win32_Service的实例,该方法位于 ManagementClass 中,返回 ManagementObjectCollection 。但是,因为 ManagementClass 的对象需要在我们不再需要它时调用Dispose方法,并且 ManagementObjectCollection ,所以在下面的代码中,

Hello,

My question is how the disposing of an object that need to call the Dispose method works. By the way, English is not my native language, so sorry if the there are gramatical errors

I want to get a list of the service that are installed in my system, for that I use WMI. First I get the instances of the Win32_Service using the GetInstance method that are in the ManagementClass that returns a ManagementObjectCollection. But since an object of the ManagementClass needs to call the Dispose method when we don't need it anymore and the same for the ManagementObjectCollection, so here in the code bellow,

var services = new ManagementClass("Win32_Service").GetInstances();

foreach (var service in services)
{
    //Some code here            
    service.Dispose();
}
wmiServiceClass.Dispose();
services.Dispose();





我可以假设CLR会处理<$ c $的实例c> ManagementClass 它是为了获取实例而创建的,或者它最好像这样做





can I assume that the CLR will dispose of the instance of the ManagementClass that it creates for getting the instance or it's better doing it like this

var wmiServiceClass = new ManagementClass("Win32_Service");
var services = wmiServiceClass.GetInstances();

foreach (var service in services)
{
    //Some code here            
    service.Dispose();
}
wmiServiceClass.Dispose();
services.Dispose();





提前致谢。



Thanks in advance.

推荐答案

问题尚不清楚。并非所有类型都需要处理,只有那些实现接口 System.IDisposable 。另外,令人困惑的是,某些类型还引入了具有相同名称的方法,与此接口无关。您确实需要检查是否已实现此接口并调用 System.IDisposable.Dispose 。它的目的可以是......任何东西;这只是一种清理方法。如果不进行此调用,则可能会遇到不同类型的问题,包括非托管内存泄漏。即使抛出异常。



确保完成此操作的方法是使用语句(不是使用声明与混淆!):

https://msdn.microsoft.com/en-us/library/aa664736%28v=vs.71%29.aspx [ ^ ]。br />
https://msdn.microsoft.com/en-us/library/yh598w02。 aspx [ ^ ]。



-SA
The concern is not clear. Not all types need disposal, only those implementing interface System.IDisposable. Also, confusingly, some types also introduce methods with the same name, not related to this interface. You really need to check up if this interface is implemented and call System.IDisposable.Dispose. The purpose of it can be… anything; this is just a method to do some clean-up. If you don't do this call, you can face different kinds of problems, including unmanaged memory leak. Even if an exception is thrown.

On way to make sure this is done is the using statement (not to be confused with using declaration!):
https://msdn.microsoft.com/en-us/library/aa664736%28v=vs.71%29.aspx[^].
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

—SA


这篇关于C#ow处理需要调用Dispose方法的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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