-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathShim.cs
More file actions
50 lines (39 loc) · 1.63 KB
/
Copy pathShim.cs
File metadata and controls
50 lines (39 loc) · 1.63 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
using System;
using System.Linq.Expressions;
using System.Reflection;
using Pose.Helpers;
namespace Pose
{
public partial class Shim
{
private Type _baseType;
private bool _setter;
internal MethodBase Original { get; }
internal Delegate Replacement { get; private set; }
internal Object Instance { get; }
internal Type Type { get; }
private Shim(MethodBase original, object instanceOrType)
{
Original = original ?? throw new ArgumentNullException(nameof(original));
if (instanceOrType is Type type)
Type = type;
else
Instance = instanceOrType;
}
public static Shim Replace(Expression<Action> expression, bool setter = false)
=> ReplaceImpl(expression, setter, null);
public static Shim Replace<T>(Expression<Func<T>> expression, bool setter = false)
=> ReplaceImpl(expression, setter, typeof(T));
private static Shim ReplaceImpl<T>(Expression<T> expression, bool setter, Type baseType)
{
MethodBase methodBase = ShimHelper.GetMethodFromExpression(expression.Body, setter, out object instance);
return new Shim(methodBase, instance) { _setter = setter, _baseType = baseType};
}
private Shim WithImpl(Delegate replacement)
{
Replacement = replacement;
ShimHelper.ValidateReplacementMethodSignature(this.Original, this.Replacement.Method, Instance?.GetType() ?? Type, _setter, _baseType);
return this;
}
}
}