forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodEvaluator.cs
More file actions
66 lines (66 loc) · 1.94 KB
/
Copy pathMethodEvaluator.cs
File metadata and controls
66 lines (66 loc) · 1.94 KB
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
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace UnityEditor.Macros
{
public static class MethodEvaluator
{
public class AssemblyResolver
{
private readonly string _assemblyDirectory;
public AssemblyResolver(string assemblyDirectory)
{
this._assemblyDirectory = assemblyDirectory;
}
public Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
string str = args.Name.Split(new char[]
{
','
})[0];
string text = Path.Combine(this._assemblyDirectory, str + ".dll");
if (File.Exists(text))
{
return Assembly.LoadFrom(text);
}
return null;
}
}
public static object Eval(string assemblyFile, string typeName, string methodName, Type[] paramTypes, object[] args)
{
string directoryName = Path.GetDirectoryName(assemblyFile);
MethodEvaluator.AssemblyResolver @object = new MethodEvaluator.AssemblyResolver(directoryName);
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(@object.AssemblyResolve);
object result;
try
{
Assembly assembly = Assembly.LoadFrom(assemblyFile);
MethodInfo method = assembly.GetType(typeName, true).GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, paramTypes, null);
if (method == null)
{
throw new ArgumentException(string.Format("Method {0}.{1}({2}) not found in assembly {3}!", new object[]
{
typeName,
methodName,
MethodEvaluator.ToCommaSeparatedString<Type>(paramTypes),
assembly.FullName
}));
}
result = method.Invoke(null, args);
}
finally
{
AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(@object.AssemblyResolve);
}
return result;
}
private static string ToCommaSeparatedString<T>(IEnumerable<T> items)
{
return string.Join(", ", (
from o in items
select o.ToString()).ToArray<string>());
}
}
}