在.net中有了一个AppDomain——应用程序域的新概念,.NET程序通过AppDomain这个媒介来运行在进程中。
我们运行一个.NET应用程序或者一个运行库宿主时,OS会首先建立一个进程,然后会在进程中加载CLR(这个加载一般是通过调用_CorExeMain或者_CorBindToRuntimeEx方法来实现),在加载CLR时会创建一个默认的AppDomain,它是CLR的运行单元,程序的Main方法就是在这里执行,这个默认的AppDomain是唯一且不能被卸载的,当该进程消灭时,默认AppDomain才会随之消失。
一个进程中可以有多个AppDomain,且它们直接是相互隔离的,我们的Assembly是不能单独执行的,它必须被加载到某个AppDomain中,要想卸载一个Assembly就只能卸载其AppDomain。
一旦Assembly被调用,在调用之前会将程序集加载到默认AppDomain,然后执行,我们就会遇到这个问题:如果我需要做替换或者删除Assembly等这些操作的时候,由于Assembly已经被默认AppDomain加载,那么对它的更改肯定是不允许的,它会弹出这样的错误:
无法删除WindowsApplication1: 访问被拒绝
请确定磁盘末被写保护
而且文件末被使用
除非你关掉作业管理服务器,然后再操作,显然这样做是很不合理的。
并且默认AppDomain是不能被卸载的,那么我们该怎么办呢,我想到的方法是动态的加载Assembly,新建一个AppDomain,让Assembly加载到这个新AppDomain中然后执行,当执行完后卸载这个新的AppDomain即可。核心思想就是:如果程序集被加载到默认应用程序域中,则当进程运行时将无法从内存中卸载该程序集。但是,如果打开另一个应用程序域来加载和执行程序集,则卸载该应用程序域时也会同时卸载程序集。使用此技术最小化长时间运行的进程的工作集。,方法如下:
1、创建程序集加载类AssemblyDynamicLoader,该类用来创建新的AppDomain,并生成用来执行.net程序的RemoteLoader类:
<div class="chcontent"> Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Collections.Generic; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Globalization; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.IO; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Reflection; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Text; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> Ark.Log; </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The local loader. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> AssemblyDynamicLoader { </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The log util. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> ILog log </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> LogManager.GetLogger(</span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(AssemblyDynamicLoader)); </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The new appdomain. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> AppDomain appDomain; </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The remote loader. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> RemoteLoader remoteLoader; </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Initializes a new instance of the class. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> AssemblyDynamicLoader() { AppDomainSetup setup </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> AppDomainSetup(); setup.ApplicationName </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ApplicationLoader</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; setup.ApplicationBase </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> AppDomain.CurrentDomain.BaseDirectory; setup.PrivateBinPath </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Path.Combine(AppDomain.CurrentDomain.BaseDirectory, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">private</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); setup.CachePath </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> setup.ApplicationBase; setup.ShadowCopyFiles </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">true</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; setup.ShadowCopyDirectories </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> setup.ApplicationBase; </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.appDomain </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> AppDomain.CreateDomain(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ApplicationLoaderDomain</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, setup); String name </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Assembly.GetExecutingAssembly().GetName().FullName; </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.remoteLoader </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (RemoteLoader)</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.appDomain.CreateInstanceAndUnwrap(name, </span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(RemoteLoader).FullName); } </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Invokes the method. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The full name. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Name of the class. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The args input. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Name of the program. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The output of excuting. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String InvokeMethod(String fullName, String className, String argsInput, String programName) { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.remoteLoader.InvokeMethod(fullName, className, argsInput, programName); </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.remoteLoader.Output; } </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Unloads this instance. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> Unload() { </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> { AppDomain.Unload(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.appDomain); </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.appDomain </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">; } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (CannotUnloadAppDomainException ex) { log.Error(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">To unload assembly error!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, ex); } } }</span></div>
2、创建RemoteLoader类,它可以在AppDomain中自由穿越,这就需要继承System.MarshalByRefObject这个抽象类,这里RemoteLoader如果不继承MarshalByRefObject类则一定会报错(在不同AppDomain间传递对象,该对象必须是可序列化的)。以RemoteLoader类做为代理来调用待执行的.net程序。
<div class="chcontent"> Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Collections.Generic; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Globalization; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.IO; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Reflection; </span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> System.Text; </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The Remote loader. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> RemoteLoader : MarshalByRefObject { </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The assembly we need. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Assembly assembly </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The output. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> String output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> String.Empty; </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Gets the output. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The output. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String Output { </span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output; } } </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Invokes the method. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The full name. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Name of the class. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> The args input. </span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> Name of the program. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> InvokeMethod(String fullName, String className, String argsInput, String programName) { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.assembly </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> String.Empty; </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.assembly </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Assembly.LoadFrom(fullName); Type pgmType </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.assembly </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) { pgmType </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.assembly.GetType(className, </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">); } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { pgmType </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Type.GetType(className, </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">); } Object[] args </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> RunJob.GetArgs(argsInput); BindingFlags defaultBinding </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> BindingFlags.DeclaredOnly </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> BindingFlags.Public </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> BindingFlags.NonPublic </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> BindingFlags.Instance </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> BindingFlags.IgnoreCase </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> BindingFlags.InvokeMethod </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> BindingFlags.Static; CultureInfo cultureInfo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> CultureInfo(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">es-ES</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> { MethodInfo methisInfo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> RunJob.GetItsMethodInfo(pgmType, defaultBinding, programName); </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">EMethod does not exist!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; } </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.IsStatic) { </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.GetParameters().Length </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">) { </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.ReturnType </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">)) { pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, cultureInfo); </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">STo call a method without return value successful.</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (String)pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, cultureInfo); } } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.ReturnType </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">)) { pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, args, cultureInfo); </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">STo call a method without return value successful.</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (String)pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, args, cultureInfo); } } } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.GetParameters().Length </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">) { </span><span style="color: rgb(0, 0, 255);">object</span><span style="color: rgb(0, 0, 0);"> pgmClass </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Activator.CreateInstance(pgmType); </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.ReturnType </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">)) { pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, pgmClass, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, cultureInfo); </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">STo call a method without return value successful.</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (String)pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, pgmClass, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, cultureInfo); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">'ymtpgm' is program's name and the return value of it must be started with 'O'. </span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 0, 0);"> } } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">object</span><span style="color: rgb(0, 0, 0);"> pgmClass </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Activator.CreateInstance(pgmType); </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (methisInfo.ReturnType </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">)) { pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, pgmClass, args, cultureInfo); </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">STo call a method without return value successful.</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (String)pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, pgmClass, args, cultureInfo); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">'ymtpgm' is program's name and the return value of it must be started with 'O'. </span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 0, 0);"> } } } } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (String)pgmType.InvokeMember(programName, defaultBinding, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, cultureInfo); } } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) { </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">E</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> e.Message; } } }</span></div>
其中的InvokeMethod方法只要提供Assembly的全名、类的全名、待执行方法的输入参数和其全名就可以执行该方法,该方法可以是带参数或不带参数,静态的或者不是静态的。
最后这样使用这两个类:
<div class="chcontent"> Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: rgb(0, 0, 0);"> AssemblyDynamicLoader loader </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> AssemblyDynamicLoader(); String output </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> loader.InvokeMethod(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">fileName</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ymtcla</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">yjoinp</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ymtpgm</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); loader.Unload(); </span></div>
-
顶
- 0
-
踩
- 0