A game about forced loneliness, made by TACStudios
1using System;
2using System.Reflection;
3
4namespace UnityEngine.UI.Tests
5{
6 class PrivateFieldSetter<T> : IDisposable
7 {
8 private object m_Obj;
9 private FieldInfo m_FieldInfo;
10 private object m_OldValue;
11
12 public PrivateFieldSetter(object obj, string field, object value)
13 {
14 m_Obj = obj;
15 m_FieldInfo = typeof(T).GetField(field, BindingFlags.NonPublic | BindingFlags.Instance);
16 m_OldValue = m_FieldInfo.GetValue(obj);
17 m_FieldInfo.SetValue(obj, value);
18 }
19
20 public void Dispose()
21 {
22 m_FieldInfo.SetValue(m_Obj, m_OldValue);
23 }
24 }
25
26 static class PrivateStaticField
27 {
28 public static T GetValue<T>(Type staticType, string fieldName)
29 {
30 var type = staticType;
31 FieldInfo field = null;
32 while (field == null && type != null)
33 {
34 field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic);
35 type = type.BaseType;
36 }
37 return (T)field.GetValue(null);
38 }
39 }
40
41 static class PrivateField
42 {
43 public static T GetValue<T>(this object o, string fieldName)
44 {
45 var type = o.GetType();
46 FieldInfo field = null;
47 while (field == null && type != null)
48 {
49 field = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
50 type = type.BaseType;
51 }
52 return field != null ? (T)field.GetValue(o) : default(T);
53 }
54 }
55}