forked from nalulululuna/NalulunaModifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionUtil.cs
104 lines (97 loc) · 2.78 KB
/
ReflectionUtil.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Reflection;
using UnityEngine;
namespace NalulunaModifier
{
public static class ReflectionUtil
{
public static void SetPrivateField(this object obj, string fieldName, object value)
{
Type t = obj.GetType();
FieldInfo fi = null;
while (t != null)
{
fi = t.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (fi != null) break;
t = t.BaseType;
}
fi.SetValue(obj, value);
}
public static T GetPrivateField<T>(this object obj, string fieldName)
{
Type t = obj.GetType();
FieldInfo fi = null;
while (t != null)
{
fi = t.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (fi != null) break;
t = t.BaseType;
}
var value = fi.GetValue(obj);
return (T)value;
}
public static void SetPrivateProperty(this object obj, string propertyName, object value)
{
Type t = obj.GetType();
PropertyInfo pi = null;
while (t != null)
{
pi = t.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (pi != null) break;
t = t.BaseType;
}
pi.SetValue(obj, value, null);
}
public static void SetNonPublicProperty(this object obj, string propertyName, object value)
{
Type t = obj.GetType();
PropertyInfo pi = null;
while (t != null)
{
pi = t.GetProperty(propertyName);
if (pi != null) break;
t = t.BaseType;
}
pi = pi.DeclaringType.GetProperty(propertyName);
pi.GetSetMethod(true).Invoke(obj, new object[] { value });
}
public static T GetPrivateProperty<T>(this object obj, string propertyName)
{
Type t = obj.GetType();
PropertyInfo pi = null;
while (t != null)
{
pi = t.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (pi != null) break;
t = t.BaseType;
}
var value = pi.GetValue(obj);
return (T)value;
}
public static void InvokePrivateMethod(this object obj, string methodName, object[] methodParams)
{
Type t = obj.GetType();
MethodInfo mi = null;
while (t != null)
{
mi = t.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
if (mi != null) break;
t = t.BaseType;
}
mi.Invoke(obj, methodParams);
}
public static Component CopyComponent(Component original, Type originalType, Type overridingType,
GameObject destination)
{
var copy = destination.AddComponent(overridingType);
var fields = originalType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
BindingFlags.GetField);
foreach (var field in fields)
{
var value = field.GetValue(original);
field.SetValue(copy, value);
}
return copy;
}
}
}