3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
C#建立DLL並且動態載入和動態卸載    資料來源:     00.http://codex.wiki/post/121123-558     01.https://dotblogs.com.tw/chou/2009/06/22/8930      02.https://social.msdn.microsoft.com/Forums/en-US/a6a896ca-8905-41fb-8f52-7f39e89c9a91/problem-loading-and-unloading-dynamically-an-assembly-dll-in-c?forum=csharplanguage   01.DLL 程式瑪 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CS_ClassLibrary1 {     public class Class1     {         public void showMessage(String data)         {             Console.WriteLine(“DLL_showMessage:\t” + data);         }     } }   02.EXE 程式瑪 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; /*  資料來源:     00.http://codex.wiki/post/121123-558     01.https://dotblogs.com.tw/chou/2009/06/22/8930      02.https://social.msdn.microsoft.com/Forums/en-US/a6a896ca-8905-41fb-8f52-7f39e89c9a91/problem-loading-and-unloading-dynamically-an-assembly-dll-in-c?forum=csharplanguage */ namespace CS_Console_DLL_test {     class Program     {         static void Main(string[] args)         {             AppDomain ad = AppDomain.CreateDomain(“Test”);             // Loader lives in another AppDomain             Loader loader = (Loader)ad.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName,typeof(Loader).FullName);             loader.LoadAssembly(@”.\CS_ClassLibrary1.dll”);             loader.ExecuteStaticMethod(“CS_ClassLibrary1.Class1”, “showMessage”, “123456”);             AppDomain.Unload(ad);             loader = null;             Console.ReadLine();         }         class Loader : MarshalByRefObject         {             private Assembly _assembly;             public override object InitializeLifetimeService()             {                 return null;             }             public void LoadAssembly(string path)             {                 _assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));             }             public object ExecuteStaticMethod(string fullClassName, string methodName, params Object[] args)             {                 if (_assembly == null)                 {                     return false;                 }                 Type tp = _assembly.GetType(fullClassName);                 if (tp == null)                 {                     return false;                 }                 MethodInfo method = tp.GetMethod(methodName);                 if (method == null)                 {                     return false;                 }                 Object obj = Activator.CreateInstance(tp);                 method.Invoke(obj, args);                 return true;              }         }     } }        

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦