-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReflections.cs
More file actions
117 lines (89 loc) · 4.35 KB
/
Reflections.cs
File metadata and controls
117 lines (89 loc) · 4.35 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace DropInMultiplayer {
//Code written by Wildbook
public static class Reflection {
private static readonly BindingFlags _defaultFlags
= BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
#region Field
public static TReturn GetFieldValue<TReturn>(this object instance, string fieldName) {
return (TReturn)instance.GetType()
.GetField(fieldName, _defaultFlags | BindingFlags.Instance)
.GetValue(instance);
}
public static TReturn GetFieldValue<TClass, TReturn>(string fieldName) {
return (TReturn)typeof(TClass)
.GetField(fieldName, _defaultFlags | BindingFlags.Static)
.GetValue(null);
}
public static void SetFieldValue(this object instance, string fieldName, object value) {
instance.GetType()
.GetField(fieldName, _defaultFlags | BindingFlags.Instance)
.SetValue(instance, value);
}
public static void SetFieldValue<TClass>(string fieldName, object value) {
typeof(TClass)
.GetField(fieldName, _defaultFlags | BindingFlags.Static)
.SetValue(null, value);
}
#endregion
#region Property
public static TReturn GetProperyValue<TReturn>(this object instance, string propName) {
return (TReturn)instance.GetType()
.GetProperty(propName, _defaultFlags | BindingFlags.Instance)
.GetValue(instance);
}
public static TReturn GetProperyValue<TClass, TReturn>(string propName) {
return (TReturn)typeof(TClass)
.GetProperty(propName, _defaultFlags | BindingFlags.Static)
.GetValue(null);
}
public static void SetProperyValue(this object instance, string propName, object value) {
instance.GetType()
.GetProperty(propName, _defaultFlags | BindingFlags.Instance)
.SetValue(instance, value);
}
public static void SetProperyValue<TClass>(string propName, object value) {
typeof(TClass).GetProperty(propName, _defaultFlags | BindingFlags.Static)
.SetValue(null, value);
}
#endregion
#region Method
public static TReturn InvokeMethod<TReturn>(this object instance, string methodName, params object[] methodParams) {
return (TReturn)instance.GetType()
.GetMethod(methodName, _defaultFlags | BindingFlags.Instance)
.Invoke(instance, methodParams);
}
public static TReturn InvokeMethod<TClass, TReturn>(string methodName, params object[] methodParams) {
return (TReturn)typeof(TClass)
.GetMethod(methodName, _defaultFlags | BindingFlags.Static)
.Invoke(null, methodParams);
}
public static void InvokeMethod(this object instance, string methodName, params object[] methodParams) {
instance.InvokeMethod<object>(methodName, methodParams);
}
public static void InvokeMethod<TClass>(string methodName, params object[] methodParams) {
InvokeMethod<TClass, object>(methodName, methodParams);
}
#endregion
#region Class
public static Type GetNestedType<TParent>(string name) {
return typeof(TParent).GetNestedType(name, BindingFlags.Public | BindingFlags.NonPublic);
}
public static object Instantiate(this Type type) {
return Activator.CreateInstance(type, true);
}
public static object InstantiateGeneric<TClass>(this Type typeArgument) {
return typeof(TClass).MakeGenericType(typeArgument).Instantiate();
}
public static object InstantiateGeneric<TClass>(this Type[] typeArgument) {
return typeof(TClass).MakeGenericType(typeArgument).Instantiate();
}
public static IList InstantiateList(this Type type) {
return (IList)typeof(List<>).MakeGenericType(type).Instantiate();
}
#endregion
}
}