Repository navigation
Expand file tree
/
Copy pathProtected.cs
More file actions
97 lines (83 loc) · 2.75 KB
/
Copy pathProtected.cs
File metadata and controls
97 lines (83 loc) · 2.75 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
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace DapperExtensions.Test.Helpers
{
public class Protected
{
private readonly object _obj;
public Protected(object obj)
{
if (obj == null)
{
throw new ArgumentException("object cannot be null.", "obj");
}
_obj = obj;
}
public static Expression IsNull<T>()
{
Expression<Func<Type>> expr = () => typeof(T);
return expr.Body;
}
public void RunMethod(string name, params object[] parameters)
{
InvokeMethod(name, null, parameters);
}
public T RunMethod<T>(string name, params object[] parameters)
{
return (T)InvokeMethod(name, null, parameters);
}
public void RunGenericMethod(string name, Type[] genericTypes, params object[] parameters)
{
InvokeMethod(name, genericTypes, parameters);
}
public TResult RunGenericMethod<TResult>(string name, Type[] genericTypes, params object[] parameters)
{
return (TResult)InvokeMethod(name, genericTypes, parameters);
}
public object InvokeMethod(string name, Type[] genericTypes, object[] parameters)
{
object[] pa = parameters.Select(p =>
{
if (p is ConstantExpression)
{
return null;
}
return p;
}).ToArray();
MethodInfo method = GetMethod(name, parameters);
try
{
if (genericTypes != null && genericTypes.Any())
{
method = method.MakeGenericMethod(genericTypes);
}
return method.Invoke(_obj, pa);
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
public MethodInfo GetMethod(string name, object[] parameters)
{
Type[] types = parameters.Select(p =>
{
if (p is ConstantExpression)
{
return (Type)((ConstantExpression)p).Value;
}
return p.GetType();
}).ToArray();
MethodInfo method = _obj.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, types, null);
if (method == null)
{
throw new ArgumentException(string.Format("{0} was not found in {1}.", name, _obj.GetType()), name);
}
return method;
}
}
}