卸载动态组件 [英] Unloading a dynamic assmebly

查看:108
本文介绍了卸载动态组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个&我内置的方法在运行时编译并成功执行.我的问题是如何卸载/重新分配该程序集,以便可以再次运行代码并重新生成程序集,而不必重新启动应用程序.我在尝试时与正在使用的文件发生命名冲突.我的程序集被编译为"dyn.dll".

这是我实例化它的方式:

I have a class & method I built in code and compiled at runtime and successfully execute. My question is how do i unload/deallocate that assembly so I can rerun the code again and rebuild the assembly without having to restart the app. I am getting a naming conflict with a file in use when I try. My assembly is compiled as "dyn.dll".

Here is how I instantiate it:

parms.OutputAssembly = "Dyn.dll";
                                
CompilerResults results = provider.CompileAssemblyFromSource(parms, SourceCode);
               
if (results.Errors.Count > 0)
     foreach (CompilerError ce in results.Errors)
            MessageBox.Show(ce.ToString());

else
{
      Assembly assem = Assembly.LoadFile(Application.StartupPath + "\\dyn.dll");
      AssemblyName assemName = assem.GetName();

      Type [] t = assem.GetTypes();
      MethodInfo [] mi = t[0].GetMethods();
      obj = t[0].InvokeMember("GetRunTimeData", BindingFlags.InvokeMethod,
                            null, null, new Object[] { pcs, milePostFormat, new DataAccess() });

                    
      bs.DataSource = obj;
     
}

第二个问题是,如果我在内存中生成该程序集,那么我似乎无法获得该程序集的句柄,因此我现在正在创建实际文件.



谢谢

A second question would be I cannot seem to get a handle on this assembly if I generate it in memory so I am creating the actual file for now. what aa I missing to do this all in memory?


Thanks

推荐答案

你好迈克尔.
/>
简短的答案是无法完成卸载/重新加载/重新生成.实际上,Reflection.Emit的优点之一是您可以生成程序集,并且即使在生成程序集后也可以对其进行修改.尽管据我所知,您也无法卸载它.

关于内存生成,有一种方法,但是我真的不记得它是怎么回事.我相信,您只需要设置params.GenerateInMemory = true;

然后,您只需使用:
汇编assem = results.CompiledAssembly;

希望对您有所帮助.
费尔南多.

Hello Michael.

The short answer is that unloading/reloading/regenerating cannot be done. In fact, one of the advantages of Reflection.Emit is that you generate an assembly and you can keep modifying it even after it's been generated. Although, as far as I know, you can't unload it either.

About generating in memory, there is a way, but I don't really remember how it was. I believe that you only need to set params.GenerateInMemory = true;

Then you simply use:
Assembly assem = results.CompiledAssembly;

Hope it helps.
Fernando.


这篇关于卸载动态组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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