forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionTools.cs
More file actions
35 lines (34 loc) · 991 Bytes
/
ReflectionTools.cs
File metadata and controls
35 lines (34 loc) · 991 Bytes
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;
public static class ReflectionTools {
// If the class is non-static
public static Object InvokePrivate (Object objectUnderTest, string method, params object [] args)
{
Type t = objectUnderTest.GetType ();
return t.InvokeMember (method,
BindingFlags.InvokeMethod |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static,
null,
objectUnderTest,
args);
}
// if the class is static
public static Object InvokePrivate (Type typeOfObjectUnderTest, string method, params object [] args)
{
MemberInfo [] members = typeOfObjectUnderTest.GetMembers (BindingFlags.NonPublic | BindingFlags.Static);
foreach (var member in members) {
if (member.Name == method) {
return typeOfObjectUnderTest.InvokeMember (method,
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.InvokeMethod,
null,
typeOfObjectUnderTest,
args);
}
}
return null;
}
}