-
Notifications
You must be signed in to change notification settings - Fork 76
/
ShimHelper.cs
130 lines (116 loc) · 6.07 KB
/
ShimHelper.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Pose.Exceptions;
using Pose.Extensions;
namespace Pose.Helpers
{
internal static class ShimHelper
{
public static MethodBase GetMethodFromExpression(Expression expression, bool setter, out Object instanceOrType)
{
switch (expression.NodeType)
{
case ExpressionType.MemberAccess:
{
MemberExpression memberExpression = expression as MemberExpression;
MemberInfo memberInfo = memberExpression.Member;
if (memberInfo.MemberType == MemberTypes.Property)
{
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
instanceOrType = GetObjectInstanceOrType(memberExpression.Expression);
return setter ? propertyInfo.GetSetMethod() : propertyInfo.GetGetMethod();
}
else
throw new NotImplementedException("Unsupported expression");
}
case ExpressionType.Call:
MethodCallExpression methodCallExpression = expression as MethodCallExpression;
instanceOrType = GetObjectInstanceOrType(methodCallExpression.Object);
return methodCallExpression.Method;
case ExpressionType.New:
NewExpression newExpression = expression as NewExpression;
instanceOrType = null;
return newExpression.Constructor;
default:
throw new NotImplementedException("Unsupported expression");
}
}
public static void ValidateReplacementMethodSignature(MethodBase original, MethodInfo replacement, Type type, bool setter)
{
bool isValueType = original.DeclaringType.IsValueType;
bool isStatic = original.IsStatic;
bool isConstructor = original.IsConstructor;
bool isStaticOrConstructor = isStatic || isConstructor;
Type vaildReturnType = isConstructor ? original.DeclaringType : (original as MethodInfo).ReturnType;
vaildReturnType = setter ? typeof(void) : vaildReturnType;
Type shimReturnType = replacement.ReturnType;
Type validOwningType = type;
Type shimOwningType = isStaticOrConstructor
? validOwningType : replacement.GetParameters().Select(p => p.ParameterType).FirstOrDefault();
var validParameterTypes = original.GetParameters().Select(p => p.ParameterType);
var shimParameterTypes = replacement.GetParameters()
.Select(p => p.ParameterType)
.Skip(isStaticOrConstructor ? 0 : 1);
if (vaildReturnType != shimReturnType)
throw new InvalidShimSignatureException("Mismatched return types");
if (!isStaticOrConstructor)
{
if (isValueType && !shimOwningType.IsByRef)
throw new InvalidShimSignatureException("ValueType instances must be passed by ref");
}
if ((isValueType && !isStaticOrConstructor ? validOwningType.MakeByRefType() : validOwningType) != shimOwningType)
throw new InvalidShimSignatureException("Mismatched instance types");
if (validParameterTypes.Count() != shimParameterTypes.Count())
throw new InvalidShimSignatureException("Parameters count do not match");
for (int i = 0; i < validParameterTypes.Count(); i++)
{
if (validParameterTypes.ElementAt(i) != shimParameterTypes.ElementAt(i))
throw new InvalidShimSignatureException($"Parameter types at {i} do not match");
}
}
public static object GetObjectInstanceOrType(Expression expression)
{
object instanceOrType = null;
switch (expression?.NodeType)
{
case ExpressionType.MemberAccess:
{
MemberExpression memberExpression = expression as MemberExpression;
ConstantExpression constantExpression = memberExpression.Expression as ConstantExpression;
if (memberExpression.Member.MemberType == MemberTypes.Field)
{
FieldInfo fieldInfo = (memberExpression.Member as FieldInfo);
var obj = fieldInfo.IsStatic ? null : constantExpression.Value;
instanceOrType = fieldInfo.GetValue(obj);
}
else if (memberExpression.Member.MemberType == MemberTypes.Property)
{
PropertyInfo propertyInfo = (memberExpression.Member as PropertyInfo);
var obj = propertyInfo.GetMethod.IsStatic ? null : constantExpression.Value;
instanceOrType = propertyInfo.GetValue(obj);
}
EnsureInstanceNotValueType(instanceOrType);
break;
}
case ExpressionType.Call:
{
MethodCallExpression methodCallExpression = expression as MethodCallExpression;
MethodInfo methodInfo = methodCallExpression.Method;
instanceOrType = methodInfo.GetGenericArguments().FirstOrDefault();
break;
}
default:
return null;
}
return instanceOrType;
}
private static void EnsureInstanceNotValueType(object instance)
{
if (instance.GetType().IsSubclassOf(typeof(ValueType)))
throw new NotSupportedException("You cannot replace methods on specific value type instances");
}
}
}