如何判断资源是否不受管理?

问题描述:

我正在使用Visual Studio 2017编写C#应用程序.我努力利用"using"语句处理所有实例化的对象.如果我实例化对象不是基于隐式可转换为'System.IDisposable'的类型,Visual Studio会警告我.此示例导致VS显示警告(C#):

I'm Using Visual Studio 2017 to write C# apps. I strive to dispose of all objects I instantiate by utilizing the "using" statement. Visual Studio warns me if I instantiate an object NOT based on a type that is implicitly convertible to 'System.IDisposable'. This example results in VS displaying the warning (C#):

using (uri = new System.Uri(stringVarWithPath))
{
}

所有未实现Dispose方法的类型都是非托管的吗?我问是因为实施处置方法"( https://docs.microsoft.com/zh-cn/dotnet/standard/garbage-collection/implementing-dispose )似乎暗示其仅适用于非托管资源.

Are all types that don't implement a Dispose method unmanaged? I ask because "Implementing a Dispose Method" (https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose) seems to imply its applicable only to unmanaged resources.

反之亦然.

首先,管理在.NET中使用的所有类型.但是某些托管类型封装了非托管资源,例如Windows句柄.

First, all types you work with in .NET are managed. But some managed types encapsulate unmanaged resources, such as Windows handles.

封装非托管资源的类型,通常实现 IDisposable .通过 IDisposable 接口,您可以通过调用 Dispose()或使用将其放入来显式释放这些对象持有的非托管资源.在后一种情况下,使用退出的范围时,会自动调用 Dispose().

The types that encapsulate unmanaged resources, usually implement IDisposable. The IDisposable interface lets you explicitly release unmanaged resources held by these objects, by either calling Dispose(), or putting them in using. In the latter case, Dispose() is called automatically when exiting the scope of the using.

无论如何,即使未在实现它的对象上调用 Dispose(),设计良好的类也应在 Finalize()中释放其非托管资源(基本上是析构函数).但是 Finalize()是由GC调用的,我们不知道GC的调用频率是多少,这使释放资源具有不确定性.如果没有 IDisposable ,我们将长时间持有昂贵的非托管资源,远远超出了必要.

In any way, even if Dispose() is not called on an object that implements it, well designed classes should release their unmanaged resources in Finalize() (which is basically destructor). But Finalize() is invoked by the GC, and we don't know how often the GC is invoked, which meakes the process of releasing resources non-deterministic. Without IDisposable, we'd hold expensive unmanaged resources for extended periods of time, much more than might be necessary.

当类型不实现 IDisposable 时,表明它不拥有非托管资源,也没有理由要明确处理(当然,前提是该类型的设计良好)).

When a type doesn't implement IDisposable, it is an indication that it holds no unmanaged resources, and it has no reason to be explicitly disposed (provided the type is well-designed, of course).

请注意,某些实现 IDisposable 的类型实际上并不保存任何非托管资源.例如,一个测量执行时间的类可以实现 IDisposable ,以便将时间戳记保存在构造函数中,然后在 Dispose 中获取当前时间戳记并计算经过时间并报告它使用某种日志记录机制.将此类放在中使用时,您将获得一种方便的方式来测量代码块的执行时间.

Note that some types that implement IDisposable don't actually hold any unmanaged resources. For example, a class that measures time of execution can implement IDisposable, so that it saves a timestamp in constructor, then in Dispose it takes current timestamp and calculates elapsed time and reports it to some logging mechanism. When you put such a class inside using, you get a convenient way of measuring execution time of your code blocks.