卸货大会装有Assembly.LoadFrom() [英] Unloading the Assembly loaded with Assembly.LoadFrom()

查看:217
本文介绍了卸货大会装有Assembly.LoadFrom()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加载的DLL后检查时间量运行GetTypes()。
在code是如下:

I need to check the time amount to run GetTypes() after loading the dll. The code is as follows.

Assembly assem = Assembly.LoadFrom(file);
sw = Stopwatch.StartNew();
var types1 = assem.GetTypes();
sw.Stop();
double time1 = sw.Elapsed.TotalMilliseconds;

我想卸载并重新加载该dll检查运行GetTypes花时间()一次。

I'd like to unload and reload the dll to check the time to spend in running GetTypes() again.


  • 如何卸载它? ASSEM = NULL 是不够好?

  • 是否有一个明确的方法来调用垃圾回收器回收分配的资源来ASSEM?

  • How can I unload it? assem = null is good enough?
  • Is there an explicit way to call garbage collector to reclaim the resource allocated to assem?

推荐答案

不幸的是,一旦它被加载,你不能卸载的组件。但是你可以卸载一个AppDomain。你可以做的是在需要的时候创建一个新的AppDomain(AppDomain.CreateDomain(...)),该组件加载到该应用程序域与它的工作,然后卸载的AppDomain。当卸载AppDomain中,已加载的所有组件将被卸载。 (请参见参考

Unfortunately you can not unload an assembly once it is loaded. But you can unload an AppDomain. What you can do is to create a new AppDomain (AppDomain.CreateDomain(...) ), load the assembly into this appdomain to work with it, and then unload the AppDomain when needed. When unloading the AppDomain, all assemblies that have been loaded will be unloaded. (See reference)

要调用垃圾收集器,可以使用

To call the garbage collector, you can use

GC.Collect(); // collects all unused memory
GC.WaitForPendingFinalizers(); // wait until GC has finished its work
GC.Collect();

GC调用后台线程的终结,这就是为什么你必须等待,再次调用收集(),以确保您删除一切。

GC calls the finalizers in a background thread, that's why you have to wait and call Collect() again to make sure you deleted everything.

这篇关于卸货大会装有Assembly.LoadFrom()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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