-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReflectionUtils.cs
36 lines (32 loc) · 1.31 KB
/
ReflectionUtils.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
using System;
using System.Reflection;
namespace MoreBeautification
{
public static class ReflectionUtils
{
/// <summary>
/// Uses reflection to get the field value from an object.
/// https://stackoverflow.com/a/3303182/1011428
/// </summary>
///
/// <param name="type">The instance type.</param>
/// <param name="instance">The instance object.</param>
/// <param name="fieldName">The field's name which is to be fetched.</param>
///
/// <returns>The field value from the object.</returns>
public static FieldInfo GetInstanceField(Type type, string fieldName)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
return type.GetField(fieldName, bindFlags);
}
public static MethodInfo GetInstanceMethod(Type type, string methodName)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
return type.GetMethod(methodName, bindFlags);
}
public static object InvokeInstanceMethod(MethodInfo method, object instance, params object[] args)
{
return method.Invoke(instance, args);
}
}
}