自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于C#中動(dòng)態(tài)加載AppDomain的問(wèn)題

開(kāi)發(fā) 后端
本文介紹了一些關(guān)于C#中動(dòng)態(tài)加載AppDomain的問(wèn)題的解決辦法。在.NET中出現(xiàn)了一個(gè)新的概念:AppDomain——應(yīng)用程序域,所有.NET應(yīng)用程序都需要運(yùn)行在托管環(huán)境中。

在操作系統(tǒng)中,利用進(jìn)程可以對(duì)正在運(yùn)行的應(yīng)用程序進(jìn)行隔離,每個(gè)應(yīng)用程序被加載到單獨(dú)的進(jìn)程中,并為其分配虛擬內(nèi)存,進(jìn)程無(wú)法直接訪問(wèn)物理內(nèi)存,只能通過(guò)操作系統(tǒng)將虛擬內(nèi)存映射到物理內(nèi)存中,并保證進(jìn)程之間的物理內(nèi)存不會(huì)重疊,但是進(jìn)程***的缺點(diǎn)就是效率問(wèn)題,尤其是進(jìn)程的切換開(kāi)銷很大,而進(jìn)程間不能共享內(nèi)存,所以不可能從一個(gè)進(jìn)程通過(guò)傳遞指針給另一個(gè)進(jìn)程。

在.NET中出現(xiàn)了一個(gè)新的概念:AppDomain——應(yīng)用程序域,所有.NET應(yīng)用程序都需要運(yùn)行在托管環(huán)境中,操作系統(tǒng)能提供的只有進(jìn)程,因此.NET程序需要通過(guò)AppDomain這個(gè)媒介來(lái)運(yùn)行在進(jìn)程中,同時(shí)使用該incheng提供的內(nèi)存空間,只要是.NET的應(yīng)用都會(huì)運(yùn)行在某個(gè)AppDomain中。

當(dāng)我們運(yùn)行一個(gè).NET應(yīng)用程序或者運(yùn)行庫(kù)宿主時(shí),OS會(huì)首先建立一個(gè)進(jìn)程,然后會(huì)在進(jìn)程中加載CLR(這個(gè)加載一般是通過(guò)調(diào)用_CorExeMain或者_(dá)CorBindToRuntimeEx方法來(lái)實(shí)現(xiàn)),在加載CLR時(shí)會(huì)創(chuàng)建一個(gè)默認(rèn)的AppDomain,它是CLR的運(yùn)行單元,程序的Main方法就是在這里執(zhí)行,這個(gè)默認(rèn)的AppDomain是唯一且不能被卸載的,當(dāng)該進(jìn)程消滅時(shí),默認(rèn)AppDomain才會(huì)隨之消失。

一個(gè)進(jìn)程中可以有多個(gè)AppDomain,且它們直接是相互隔離的,我們的Assembly是不能單獨(dú)執(zhí)行的,它必須被加載到某個(gè)AppDomain中,要想卸載一個(gè)Assembly就只能卸載其AppDomain。

最近在我所參加的一個(gè)項(xiàng)目中要實(shí)現(xiàn)這樣一個(gè)模塊:定制一個(gè)作業(yè)管理器,它可以定時(shí)的以不同頻率執(zhí)行某些.Net應(yīng)用程序或者存儲(chǔ)過(guò)程,這里的頻率可以是僅一次、每天、每周還是每月進(jìn)行執(zhí)行計(jì)劃的實(shí)施,對(duì)于調(diào)用存儲(chǔ)過(guò)程沒(méi)什么好說(shuō)的,但是調(diào)用.Net應(yīng)用程序的時(shí)候就需要考慮如下問(wèn)題:

一旦Assembly被作業(yè)管理器的服務(wù)器調(diào)用,(比如某個(gè)執(zhí)行計(jì)劃正好要被執(zhí)行了),在調(diào)用之前會(huì)將程序集加載到默認(rèn)AppDomain,然后執(zhí)行,這就有個(gè)問(wèn)題,如果我需要做替換或者刪除Assembly等這些操作的時(shí)候,由于Assembly已經(jīng)被默認(rèn)AppDomain加載,那么對(duì)它的更改肯定是不允許的,它會(huì)彈出這樣的錯(cuò)誤:

錯(cuò)誤提示

除非你關(guān)掉作業(yè)管理服務(wù)器,然后再操作,顯然這樣做是很不合理的。

并且默認(rèn)AppDomain是不能被卸載的,那么我們?cè)撛趺崔k呢,我想到的方法是動(dòng)態(tài)的加載Assembly,新建一個(gè)AppDomain,讓Assembly加載到這個(gè)新AppDomain中然后執(zhí)行,當(dāng)執(zhí)行完后卸載這個(gè)新的AppDomain即可,方法如下:

1、創(chuàng)建程序集加載類AssemblyDynamicLoader,該類用來(lái)創(chuàng)建新的AppDomain,并生成用來(lái)執(zhí)行.Net程序的RemoteLoader類:

  1.  using System;  
  2.  
  3.     using System.Collections.Generic;  
  4.     using System.Globalization;  
  5.     using System.IO;  
  6.     using System.Reflection;  
  7.     using System.Text;  
  8.     using Ark.Log;  
  9.  
  10.     /// < summary>  
  11.     /// The local loader.  
  12.     /// < /summary>  
  13.     public class AssemblyDynamicLoader  
  14.     {  
  15.   /// < summary>  
  16.   /// The log util.  
  17.   /// < /summary>  
  18.   private static ILog log = LogManager.GetLogger(typeof(AssemblyDynamicLoader));  
  19.  
  20.   /// < summary>  
  21.   /// The new appdomain.  
  22.   /// < /summary>  
  23.   private AppDomain appDomain;  
  24.  
  25.   /// < summary>  
  26.   /// The remote loader.  
  27.   /// < /summary>  
  28.   private RemoteLoader remoteLoader;  
  29.  
  30.   /// < summary>  
  31.   /// Initializes a new instance of the < see cref="LocalLoader"/> class.  
  32.   /// < /summary>  
  33.   public AssemblyDynamicLoader()  
  34.   {  
  35. AppDomainSetup setup = new AppDomainSetup();  
  36. setup.ApplicationName = "ApplicationLoader";  
  37. setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;  
  38. setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");  
  39. setup.CachePath = setup.ApplicationBase;  
  40. setup.ShadowCopyFiles = "true";  
  41. setup.ShadowCopyDirectories = setup.ApplicationBase;  
  42.  
  43. this.appDomain = AppDomain.CreateDomain("ApplicationLoaderDomain"null, setup);  
  44. String name = Assembly.GetExecutingAssembly().GetName().FullName;  
  45.  
  46. this.remoteLoader = (RemoteLoader)this.appDomain.CreateInstanceAndUnwrap(name, typeof(RemoteLoader).FullName);  
  47.   }  
  48.  
  49.   /// < summary>  
  50.   /// Invokes the method.  
  51.   /// < /summary>  
  52.   /// < param name="fullName">The full name.< /param>  
  53.   /// < param name="className">Name of the class.< /param>  
  54.   /// < param name="argsInput">The args input.< /param>  
  55.   /// < param name="programName">Name of the program.< /param>  
  56.   /// < returns>The output of excuting.< /returns>  
  57.   public String InvokeMethod(String fullName, String className, String argsInput, String programName)  
  58.   {  
  59. this.remoteLoader.InvokeMethod(fullName, className, argsInput, programName);  
  60. return this.remoteLoader.Output;  
  61.   }  
  62.  
  63.   /// < summary>  
  64.   /// Unloads this instance.  
  65.   /// < /summary>  
  66.   public void Unload()  
  67.   {  
  68. try 
  69. {  
  70.     AppDomain.Unload(this.appDomain);  
  71.     this.appDomain = null;  
  72. }  
  73. catch (CannotUnloadAppDomainException ex)  
  74. {  
  75.     log.Error("To unload assembly error!", ex);  
  76. }  
  77. }  
  78. }  

2、創(chuàng)建RemoteLoader類,它可以在AppDomain中自由穿越,這就需要繼承System.MarshalByRefObject這個(gè)抽象類,這里RemoteLoader如果不繼承MarshalByRefObject類則一定會(huì)報(bào)錯(cuò)(在不同AppDomain間傳遞對(duì)象,該對(duì)象必須是可序列化的)。以RemoteLoader類做為代理來(lái)調(diào)用待執(zhí)行的.Net程序。

  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Globalization;  
  4.  using System.IO;  
  5.  using System.Reflection;  
  6.  using System.Text;  
  7.  
  8.  /// < summary>  
  9.  /// The Remote loader.  
  10.  /// < /summary>  
  11.  public class RemoteLoader : MarshalByRefObject  
  12.  {  
  13.   /// < summary>  
  14.   /// The assembly we need.  
  15.   /// < /summary>  
  16.   private Assembly assembly = null;  
  17.  
  18.   /// < summary>  
  19.   /// The output.  
  20.   /// < /summary>  
  21.   private String output = String.Empty;  
  22.  
  23.   /// < summary>  
  24.   /// Gets the output.  
  25.   /// < /summary>  
  26.   /// < value>The output.< /value>  
  27.   public String Output  
  28.   {  
  29. get 
  30. {  
  31.  return this.output;  
  32. }  
  33.   }  
  34.  
  35.   /// < summary>  
  36.   /// Invokes the method.  
  37.   /// < /summary>  
  38.   /// < param name="fullName">The full name.< /param>  
  39.   /// < param name="className">Name of the class.< /param>  
  40.   /// < param name="argsInput">The args input.< /param>  
  41.   /// < param name="programName">Name of the program.< /param>  
  42.   public void InvokeMethod(String fullName, String className, String argsInput, String programName)  
  43.   {  
  44. this.assembly = null;  
  45. this.output = String.Empty;  
  46.  
  47. try 
  48. {  
  49.  this.assembly = Assembly.LoadFrom(fullName);  
  50.  
  51.  Type pgmType = null;  
  52.  if (this.assembly != null)  
  53.  {  
  54.   pgmType = this.assembly.GetType(className, truetrue);  
  55.  }  
  56.  else 
  57.  {  
  58.   pgmType = Type.GetType(className, truetrue);  
  59.  }  
  60.  
  61.  Object[] args = RunJob.GetArgs(argsInput);  
  62.  
  63.  BindingFlags defaultBinding = BindingFlags.DeclaredOnly | BindingFlags.Public  
  64. | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase  
  65. | BindingFlags.InvokeMethod | BindingFlags.Static;  
  66.  
  67.  CultureInfo cultureInfo = new CultureInfo("es-ES"false);  
  68.  
  69.  try 
  70.  {  
  71.   MethodInfo methisInfo = RunJob.GetItsMethodInfo(pgmType, defaultBinding, programName);  
  72.   if (methisInfo == null)  
  73.   {  
  74. this.output = "EMethod does not exist!";  
  75.   }  
  76.  
  77.   if (methisInfo.IsStatic)  
  78.   {  
  79. if (methisInfo.GetParameters().Length == 0)  
  80. {  
  81.  if (methisInfo.ReturnType == typeof(void))  
  82.  {  
  83.   pgmType.InvokeMember(programName, defaultBinding, nullnullnull, cultureInfo);  
  84.   this.output = "STo call a method without return value successful.";  
  85.  }  
  86.  else 
  87.  {  
  88.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, nullnullnull, cultureInfo);  
  89.  }  
  90. }  
  91. else 
  92. {  
  93.  if (methisInfo.ReturnType == typeof(void))  
  94.  {  
  95.   pgmType.InvokeMember(programName, defaultBinding, nullnull, args, cultureInfo);  
  96.   this.output = "STo call a method without return value successful.";  
  97.  }  
  98.  else 
  99.  {  
  100.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, nullnull, args, cultureInfo);  
  101.  }  
  102. }  
  103.   }  
  104.   else 
  105.   {  
  106. if (methisInfo.GetParameters().Length == 0)  
  107. {  
  108.  object pgmClass = Activator.CreateInstance(pgmType);  
  109.  
  110.  if (methisInfo.ReturnType == typeof(void))  
  111.  {  
  112.   pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, null, cultureInfo);  
  113.   this.output = "STo call a method without return value successful.";  
  114.  }  
  115.  else 
  116.  {  
  117.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, null, cultureInfo);//'ymtpgm' is program's name and the return value of it must be started with 'O'.  
  118.  }  
  119. }  
  120. else 
  121. {  
  122.  object pgmClass = Activator.CreateInstance(pgmType);  
  123.  
  124.  if (methisInfo.ReturnType == typeof(void))  
  125.  {  
  126.   pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, args, cultureInfo);  
  127.   this.output = "STo call a method without return value successful.";  
  128.  }  
  129.  else 
  130.  {  
  131.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, args, cultureInfo);//'ymtpgm' is program's name and the return value of it must be started with 'O'.  
  132.  }  
  133. }  
  134.   }  
  135.  }  
  136.  catch 
  137.  {  
  138.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, nullnullnull, cultureInfo);  
  139.  }  
  140. }  
  141. catch (Exception e)  
  142. {  
  143.  this.output = "E" + e.Message;  
  144. }  
  145. }  
  146. }   

其中的InvokeMethod方法只要提供Assembly的全名、類的全名、待執(zhí)行方法的輸入?yún)?shù)和其全名就可以執(zhí)行該方法,該方法可以是帶參數(shù)或不帶參數(shù),靜態(tài)的或者不是靜態(tài)的。

***這樣使用這兩個(gè)類:

  1. AssemblyDynamicLoader loader = new AssemblyDynamicLoader();  
  2. String output = loader.InvokeMethod("fileName""ymtcla""yjoinp""ymtpgm");  
  3.  loader.Unload();  

【編輯推薦】

  1. 淺談C#泛型的用處
  2. 淺談C#如何實(shí)現(xiàn)多繼承
  3. C#語(yǔ)言與Java語(yǔ)言程序的比較
  4. 利用C#指針進(jìn)行圖像操作
  5. C#中用鼠標(biāo)移動(dòng)頁(yè)面功能的實(shí)現(xiàn)
責(zé)任編輯:yangsai 來(lái)源: 博客園
相關(guān)推薦

2009-08-28 16:14:26

C#實(shí)現(xiàn)加載動(dòng)態(tài)庫(kù)

2009-07-31 14:47:22

JavaScript函C#

2009-02-05 15:32:23

接口委托

2009-02-03 09:33:26

動(dòng)態(tài)類型動(dòng)態(tài)編程C# 4.0

2009-08-11 14:26:56

C#動(dòng)態(tài)調(diào)用WebSe

2021-02-06 10:27:45

C#函數(shù)參數(shù)

2022-01-14 07:56:39

C#動(dòng)態(tài)查詢

2009-09-02 10:58:02

C#動(dòng)態(tài)數(shù)組

2009-08-12 16:01:32

C#動(dòng)態(tài)改變數(shù)據(jù)

2009-08-24 16:11:35

C#項(xiàng)目開(kāi)發(fā)

2010-06-01 13:32:15

Visual Stud

2011-06-09 09:08:00

C#循環(huán)結(jié)構(gòu)

2009-09-02 11:02:57

C#動(dòng)態(tài)數(shù)組

2009-09-17 18:07:22

C#動(dòng)態(tài)數(shù)組

2012-12-26 09:31:44

C#Winform

2009-08-17 17:08:47

C#轉(zhuǎn)義

2009-09-17 18:14:05

C#動(dòng)態(tài)數(shù)組

2009-08-27 16:29:18

C#動(dòng)態(tài)編譯

2009-09-17 17:44:51

C#動(dòng)態(tài)數(shù)組

2009-09-17 17:40:36

C#動(dòng)態(tài)數(shù)組
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)