zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    SWPUCTF2019 | 神奇的二维码
    CG-CTF | Hello,RE!
    CG-CTF | I wanna play CTF
    CG-CTF | MD5
    CG-CTF | 综合题2
    CG-CTF | 密码重置2
    CG-CTF | 综合题
    修改input输入框placeholder文字默认颜色
    解决前端浏览器字体小于12px办法
    box-shadow四个边框设置阴影样式
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/12748215.html
Copyright © 2011-2022 走看看