• unity跨平台及热更新学习笔记-C#中通过程序域实现DLL的动态加载与卸载


    创建C#控制台程序,项目结构如下:

    HotUpdate是一个控制台应用程序,CommonLib、MoudleAdditive是两个类库。

    CommonLib中的代码:

    1 using System;
    2 
    3 namespace CommonLib
    4 {
    5     public interface ICalculater
    6     {
    7         int Calculater(int a, int b);
    8     }
    9 }

    MoudleAdditive中的代码:

     1 using CommonLib;
     2 using System;
     3 
     4 namespace MoudleAdditive
     5 {
     6     public class MoudleAdditive : MarshalByRefObject, ICalculater
     7     {
     8         public int Calculater(int a, int b)
     9         {
    10             int res = a + b;
    11             Console.WriteLine("相加结果:" + res + " [Current Domain :" + AppDomain.CurrentDomain.FriendlyName + "]");
    12             return res;
    13         }
    14     }
    15 }

    HotUpdate中的代码:

     1 using CommonLib;
     2 using System;
     3 using System.IO;
     4 
     5 namespace HotUpdate
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             Console.WriteLine("Current Domain : " + AppDomain.CurrentDomain.FriendlyName + Environment.NewLine);
    12             Console.WriteLine("输入两个数字(a,b),计算结果:");
    13             Console.Write("a = ");
    14             int a = Convert.ToInt32(Console.ReadLine());
    15             Console.Write("b = ");
    16             int b = Convert.ToInt32(Console.ReadLine());
    17 
    18             AppDomain additiveDomain = AppDomain.CreateDomain("Domain #Calculater");
    19             ICalculater calculater = (ICalculater)additiveDomain.CreateInstanceAndUnwrap("MoudleAdditive", "MoudleAdditive.MoudleAdditive");
    20             calculater.Calculater(a, b);
    21 
    22             Console.WriteLine("输入 quit 退出,输入 delete 删除当前AppDomain,输入 reload 重新加载 AppDomain");
    23             string command;
    24             while ((command = Console.ReadLine().ToLower()) != "quit")
    25             {
    26                 switch (command)
    27                 {
    28                     case "delete":
    29                         TryDelete("MoudleAdditive.dll");
    30                         Console.WriteLine();
    31                         break;
    32                     case "unload":
    33                         UnloadDomain(additiveDomain);
    34                         Console.WriteLine();
    35                         break;
    36                     case "reload":
    37                         AppDomain appDomain;
    38                         if (ReloadDomain(out appDomain))
    39                         {
    40                             ICalculater cal = (ICalculater)appDomain.CreateInstanceAndUnwrap("MoudleAdditive", "MoudleAdditive.MoudleAdditive");
    41                             cal.Calculater(a, b);
    42                         }
    43                         Console.WriteLine();
    44                         break;
    45                     default:
    46                         break;
    47                 }
    48             }
    49 
    50             Console.ReadKey();
    51         }
    52 
    53         static void TryDelete(string filename)
    54         {
    55             try
    56             {
    57                 string fullName = AppDomain.CurrentDomain.BaseDirectory + filename;
    58                 File.Delete(fullName);
    59                 Console.WriteLine("成功删除文件 :{0}", fullName);
    60             }
    61             catch (Exception e)
    62             {
    63                 Console.WriteLine("删除文件: {0} 失败,{1}", filename, e.Message);
    64             }
    65         }
    66 
    67         static void UnloadDomain(AppDomain appDomain)
    68         {
    69             if (null == appDomain) return;
    70             string appDomainName = appDomain.FriendlyName;
    71             AppDomain.Unload(appDomain);
    72             Console.WriteLine("卸载程序域 {0} 成功!", appDomainName);
    73         }
    74 
    75         static bool ReloadDomain(out AppDomain appDomain)
    76         {
    77             try
    78             {
    79                 appDomain = AppDomain.CreateDomain("Domain #Calculater");
    80                 Console.WriteLine("Domain reloaded, name : Domain #Calculater");
    81                 return true;
    82             }
    83             catch
    84             {
    85                 appDomain = null;
    86                 return false;
    87             }
    88         }
    89     }
    90 }

    详细原理,请参看:https://www.cnblogs.com/Leo_wl/p/4255533.html

  • 相关阅读:
    (转)Windows平台 杀死端口号进程
    android 基础知识总结
    使用安卓手机来运行与调试Android应用程序的步骤
    (转)JavaScript的eval(“{JSON object string}”)问题
    (转)Android手机连接电脑详细图文教程
    Nginx配置中FastCGI的几个命令
    设置nginx的超时时间
    Nginx配置中FastCGI的几个命令
    启用django的gzip压缩支持
    启用django的gzip压缩支持
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/12748215.html
Copyright © 2020-2023  润新知