This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ReflectionHelper.cs
75 lines (64 loc) · 3.08 KB
/
ReflectionHelper.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
using System;
using System.Reflection;
namespace ReflectionHelper
{
public static class Helper
{
public static object InvokePrivateMethode(object instance, string methodname, object[] parameters)
{
Type type = instance.GetType();
MethodInfo methodInfo = type.GetMethod(methodname, BindingFlags.NonPublic | BindingFlags.Instance);
return methodInfo.Invoke(instance, parameters);
}
public static object InvokePrivateMethode(object instance, string methodname, object[] parameters, Type[] types)
{
Type type = instance.GetType();
MethodInfo methodInfo = type.GetMethod(methodname, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
return methodInfo.Invoke(instance, parameters);
}
public static void SetPrivateProperty(object instance, string propertyname, object value)
{
Type type = instance.GetType();
PropertyInfo property = type.GetProperty(propertyname, BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(instance, value, null);
}
public static object GetPrivateProperty(object instance, string propertyname)
{
Type type = instance.GetType();
PropertyInfo property = type.GetProperty(propertyname, BindingFlags.NonPublic | BindingFlags.Instance);
return property.GetValue(instance, new object[] { });
}
public static void SetPrivateProperty(Type type, string propertyname, object value)
{
PropertyInfo property = type.GetProperty(propertyname, BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(type, value, null);
}
public static object GetPrivateProperty(Type type, string propertyname)
{
PropertyInfo property = type.GetProperty(propertyname, BindingFlags.NonPublic | BindingFlags.Instance);
return property.GetValue(type, new object[] { });
}
public static void SetPrivateField(object instance, string fieldname, object value)
{
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(instance, value);
}
public static object GetPrivateField(object instance, string fieldname)
{
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Instance);
return field.GetValue(instance);
}
public static void SetPrivateField(Type type, string fieldname, object value)
{
FieldInfo field = type.GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(type, value);
}
public static object GetPrivateField(Type type, string fieldname)
{
FieldInfo field = type.GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Instance);
return field.GetValue(type);
}
}
}