Unity cross-platform and hot update study notes (1)

In C #, the dynamic loading and unloading of DLL is realized through the program domain.

Create a C # console program, the project structure is as follows:

HotUpdate is a console application, CommonLib and MoudleAdditive are two class libraries.

Code in CommonLib:

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

Code in 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 }

Code in 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 ( " Enter two numbers (a, b), calculation result: " );
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 ( " Enter quit to exit, enter delete to delete the current AppDomain, enter reload to 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 ( " Successfully deleted file: {0} " , fullName) ;
 60              }
 61              catch (Exception e)
 62              {
 63                  Console.WriteLine ( " Delete file: {0} failed, {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 }

For detailed principles, please refer to: https://www.cnblogs.com/Leo_wl/p/4255533.html

 

Guess you like

Origin www.cnblogs.com/luguoshuai/p/12748215.html