C#中单个程序集无法卸载的原因是什么
在 C# 中(或者通常在 .NET 中),单个程序集无法从内存中卸载。 卸载只能在 AppDomain 级别进行。
我想知道这种设计背后的原因是什么?其他语言支持此功能(我认为是 C++)
In C# (or maybe in .NET in general) individual assemblies cannot be unloaded from memory.
Unloading can only occur at the AppDomain level.
I am wondering what are there reasons behind this design? Other languages support this feature (C++ i think)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一篇 MSDN 博客文章,列出了一些原因为什么不呢。主要问题是:
我将用更高级的语言来总结这一点:
基本上,如果您只是删除可执行代码,那么在非托管级别上就会出错。您将编译的代码指向不再存在的其他已编译的代码,因此您的代码将跳转到无效的区域,并且可能包含任意数据。
这在托管代码中是不可接受的,因为事物应该是安全的并且有一些保证。这些保证之一是您的代码无法执行内存的任意部分。
为了正确处理这个问题,您必须更密切地跟踪更多的事情,这将是一个很大的开销。另一种方法是仅在应用程序域边界跟踪这些内容,这就是所做的事情。
Here is an MSDN blog post listing some reasons why not. The main issue is:
I'll summarise this in higher-level language:
Basically, things that go wrong if you simply delete executable code go wrong on the unmanaged level. You would have compiled code that points to other compiled code that is no longer there, so your code would jump into an area that is invalid, and possibly contains arbitrary data.
This is unacceptable in managed code, because things are meant to be safe and have some guarantees around them. One of these guarantees is that your code can't execute arbitrary sections of memory.
To handle this issue properly you'd have to track many more things more closely, and this would be a large overhead. The alternative is to only track these things at appdomain boundaries, which is what is done.