Skip to content

Instantly share code, notes, and snippets.

@swkwon
Last active March 22, 2019 05:47
Show Gist options
  • Save swkwon/8049415 to your computer and use it in GitHub Desktop.
Save swkwon/8049415 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace MyProgram
{
class Program
{
public static void Load()
{
if (Domain != null)
AppDomain.Unload(Domain);
string source = Path.Combine(m_kFilePath, m_kUpdateFolder, m_kFileName);
string dest = Path.Combine(m_kFilePath, m_kTargetFolder, m_kFileName);
if (File.Exists(source))
{
File.Copy(source, dest, true);
File.Delete(source);
}
Domain = AppDomain.CreateDomain(m_kDomainName);
loader = (Loader)Domain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);
loader.LoadAssembly(dest);
loader.Initialize(1, "DllLibrary.MyClass", "ReturnInputValue");
}
static void InvokeMethod()
{
while (true)
{
try
{
invokeEvent.WaitOne();
Console.WriteLine(loader.ExecuteStaticMethod(1, BitConverter.GetBytes(10)));
Thread.Sleep(100);
}
catch (AppDomainUnloadedException ex)
{
Console.WriteLine(ex.Message);
}
}
}
static void CheckUpdate()
{
while (true)
{
if (File.Exists(Path.Combine(m_kFilePath, m_kUpdateFolder, m_kFileName)))
{
invokeEvent.Reset();
Load();
invokeEvent.Set();
}
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
Load();
Thread t = new Thread(CheckUpdate);
Thread t1 = new Thread(InvokeMethod);
t.Start();
t1.Start();
t.Join();
t1.Join();
mainThreadEvent.WaitOne();
}
private static Loader loader;
private static AppDomain Domain;
private static ManualResetEvent invokeEvent = new ManualResetEvent(true);
private static ManualResetEvent mainThreadEvent = new ManualResetEvent(false);
private const string m_kDomainName = "myProgram";
private const string m_kUpdateFolder = "update";
private const string m_kTargetFolder = "DLLs";
private const string m_kFilePath = "D:\\TestProject\\MyProgram\\MyProgram\\bin\\Debug";
private const string m_kFileName = "DllLibrary.dll";
}
class Loader : MarshalByRefObject
{
public override object InitializeLifetimeService()
{
return null;
}
public void LoadAssembly(string path)
{
_assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));
}
public void Initialize(int i, string typeName, string methodName)
{
Type type = _assembly.GetType(typeName);
MethodInfo method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
if (!methodMap.ContainsKey(i))
methodMap.Add(i, (Func<byte[], int>)Delegate.CreateDelegate(typeof(Func<byte[], int>), method));
else
methodMap[i] = (Func<byte[], int>)Delegate.CreateDelegate(typeof(Func<byte[], int>), method);
}
public int ExecuteStaticMethod(int key, byte[] parameters)
{
int ret = 0;
if (methodMap.ContainsKey(key))
ret = methodMap[key](parameters);
return ret;
}
private Assembly _assembly;
private Dictionary<int, Func<byte[], int>> methodMap = new Dictionary<int, Func<byte[], int>>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment