处理模式:我如何知道管理的内容和未管理的内容?

时间:2022-11-17 07:29:55

Reading about the Dispose pattern, I see the documentation repeatedly refer to "cleaning up managed and unmanaged code". And in the canonical implementation of the Dispose method, I see specific flows (depending on whether disposing is true or false) dedicated to the cleanup of managed objects versus unmanaged objects.

阅读Dispose模式,我看到文档反复提到“清理托管和非托管代码”。在Dispose方法的规范实现中,我看到了专门用于清理托管对象与非托管对象的特定流程(取决于处置是真还是假)。

But am I, the lowly newbie, to know which types are managed and which are unmanaged?

但我是不是新手,知道哪些类型是管理的,哪些是不受管理的?

5 个解决方案

#1


The short version is: anything that also implements IDisposable needs to be called in your Dispose method. FxCop will also tell you if you're missing something (or not using IDisposable at all when you should be).

简短版本是:需要在Dispose方法中调用任何也实现IDisposable的东西。 FxCop还会告诉你是否遗漏了某些东西(或者根本不使用IDisposable)。

#2


Unmanaged means native Win32 objects, chiefly handles; and references to raw COM objects. These are resources that are not under the control of (or managed by) the .NET CLR.

Unmanaged意味着本机Win32对象,主要是处理;和对原始COM对象的引用。这些资源不受.NET CLR的控制(或由.NET CLR管理)。

#3


Managed or unmanaged doesn't really matter. If a class implements the IDisposable interface, you should be calling Dispose() when you're done with the object. Alternatively (preferably) make use of the using statement to have Dispose() called automatically when the object falls out of scope.

托管或非托管并不重要。如果一个类实现了IDisposable接口,那么在完成该对象后应该调用Dispose()。或者(最好)使用using语句在对象超出范围时自动调用Dispose()。

@ Rob:
The answer is still the same. If your class manages any internal objects that implement IDisposable, it should be implementing IDisposable as well. In your Dispose() method, call Dispose on those objects.

@Rob:答案仍然是一样的。如果您的类管理实现IDisposable的任何内部对象,那么它也应该实现IDisposable。在Dispose()方法中,对这些对象调用Dispose。

#4


I'd simply suggest destroying all resources after you use them. Anything that usually depends on a system resource such as sockets and stream resources you want to explicitly release. When in doubt go ahead and dispose. Saves you a lot of debugging trouble in the long term. Usually when you call code that isn't written in .NET you can assume it's not "managed code."

我建议您在使用它们之后销毁所有资源。通常依赖于系统资源的任何内容,例如要显式发布的套接字和流资源。如有疑问,请继续处理。从长远来看,可以为您节省大量的调试麻烦。通常,当您调用非.NET编写的代码时,您可以认为它不是“托管代码”。

#5


If you don't know, the types you're using are probably managed.

如果您不知道,您正在使用的类型可能已被管理。

Unmanaged types refer to types that are not safe, i.e. not conforming to the CLR safety requirements.

非托管类型是指不安全的类型,即不符合CLR安全要求。

Great definition linked:

伟大的定义链接:

Update

I don't understand the downvote? The question was specifically how to differentiate between managed and unmanaged types?

我不理解downvote?问题是具体如何区分托管类型和非托管类型?

All of the other answers were addressing the IDispose question, rather than the managed/unmanaged question!?

所有其他答案都是针对IDispose问题,而不是托管/非托管问题!?

Update 2

Still no explanation of the second downvote...

仍然没有解释第二个downvote ...

I agree, an IDisposable object should always be disposed, but that doesn't answer the question about managed vs. unmanaged.

我同意,应始终处理IDisposable对象,但这不会回答有关托管与非托管的问题。

#1


The short version is: anything that also implements IDisposable needs to be called in your Dispose method. FxCop will also tell you if you're missing something (or not using IDisposable at all when you should be).

简短版本是:需要在Dispose方法中调用任何也实现IDisposable的东西。 FxCop还会告诉你是否遗漏了某些东西(或者根本不使用IDisposable)。

#2


Unmanaged means native Win32 objects, chiefly handles; and references to raw COM objects. These are resources that are not under the control of (or managed by) the .NET CLR.

Unmanaged意味着本机Win32对象,主要是处理;和对原始COM对象的引用。这些资源不受.NET CLR的控制(或由.NET CLR管理)。

#3


Managed or unmanaged doesn't really matter. If a class implements the IDisposable interface, you should be calling Dispose() when you're done with the object. Alternatively (preferably) make use of the using statement to have Dispose() called automatically when the object falls out of scope.

托管或非托管并不重要。如果一个类实现了IDisposable接口,那么在完成该对象后应该调用Dispose()。或者(最好)使用using语句在对象超出范围时自动调用Dispose()。

@ Rob:
The answer is still the same. If your class manages any internal objects that implement IDisposable, it should be implementing IDisposable as well. In your Dispose() method, call Dispose on those objects.

@Rob:答案仍然是一样的。如果您的类管理实现IDisposable的任何内部对象,那么它也应该实现IDisposable。在Dispose()方法中,对这些对象调用Dispose。

#4


I'd simply suggest destroying all resources after you use them. Anything that usually depends on a system resource such as sockets and stream resources you want to explicitly release. When in doubt go ahead and dispose. Saves you a lot of debugging trouble in the long term. Usually when you call code that isn't written in .NET you can assume it's not "managed code."

我建议您在使用它们之后销毁所有资源。通常依赖于系统资源的任何内容,例如要显式发布的套接字和流资源。如有疑问,请继续处理。从长远来看,可以为您节省大量的调试麻烦。通常,当您调用非.NET编写的代码时,您可以认为它不是“托管代码”。

#5


If you don't know, the types you're using are probably managed.

如果您不知道,您正在使用的类型可能已被管理。

Unmanaged types refer to types that are not safe, i.e. not conforming to the CLR safety requirements.

非托管类型是指不安全的类型,即不符合CLR安全要求。

Great definition linked:

伟大的定义链接:

Update

I don't understand the downvote? The question was specifically how to differentiate between managed and unmanaged types?

我不理解downvote?问题是具体如何区分托管类型和非托管类型?

All of the other answers were addressing the IDispose question, rather than the managed/unmanaged question!?

所有其他答案都是针对IDispose问题,而不是托管/非托管问题!?

Update 2

Still no explanation of the second downvote...

仍然没有解释第二个downvote ...

I agree, an IDisposable object should always be disposed, but that doesn't answer the question about managed vs. unmanaged.

我同意,应始终处理IDisposable对象,但这不会回答有关托管与非托管的问题。