forked from SteffanDonal/BeatSaber-Claws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionUtility.cs
34 lines (30 loc) · 1.26 KB
/
ReflectionUtility.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
using System;
using System.Reflection;
namespace Claws
{
internal static class ReflectionUtility
{
public static void SetPrivateField(this object target, string fieldName, object value)
{
var field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
field.SetValue(target, value);
}
public static T GetPrivateField<T>(this object target, string fieldName)
{
var field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
var value = field.GetValue(target);
return (T)value;
}
public static object GetPrivateField(Type targetType, object targetInstance, string fieldName)
{
var field = targetInstance.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
var value = field.GetValue(targetInstance);
return value;
}
public static void InvokePrivateMethod(this object target, string methodName, object[] methodParams)
{
var method = target.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(target, methodParams);
}
}
}