-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathDomainData.cs
61 lines (45 loc) · 1.59 KB
/
DomainData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ClrLoader
{
using static ClrLoader;
class DomainData : IDisposable
{
public delegate int EntryPoint(IntPtr buffer, int size);
bool _disposed = false;
public AppDomain Domain { get; }
public Dictionary<(string, string, string), EntryPoint> _delegates;
public DomainData(AppDomain domain)
{
Domain = domain;
_delegates = new Dictionary<(string, string, string), EntryPoint>();
}
public EntryPoint GetEntryPoint(string assemblyPath, string typeName, string function)
{
if (_disposed)
throw new InvalidOperationException("Domain is already disposed");
var key = (assemblyPath, typeName, function);
EntryPoint result;
if (!_delegates.TryGetValue(key, out result))
{
var assembly = Domain.Load(AssemblyName.GetAssemblyName(assemblyPath));
var type = assembly.GetType(typeName, throwOnError: true);
Print($"Loaded type {type}");
result = (EntryPoint)Delegate.CreateDelegate(typeof(EntryPoint), type, function);
_delegates[key] = result;
}
return result;
}
public void Dispose()
{
if (!_disposed)
{
_delegates.Clear();
if (Domain != AppDomain.CurrentDomain)
AppDomain.Unload(Domain);
_disposed = true;
}
}
}
}