-
Notifications
You must be signed in to change notification settings - Fork 10
/
ProxyImplementor.cs
62 lines (50 loc) · 2.55 KB
/
ProxyImplementor.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
using System;
using System.Reflection;
using System.Reflection.Emit;
namespace LinFu.DynamicProxy
{
internal class ProxyImplementor
{
private FieldBuilder field;
public FieldBuilder InterceptorField
{
get { return field; }
}
public void ImplementProxy(TypeBuilder typeBuilder)
{
// Implement the IProxy interface
typeBuilder.AddInterfaceImplementation(typeof (IProxy));
field = typeBuilder.DefineField("__interceptor", typeof (IInterceptor),
FieldAttributes.Private);
// Implement the getter
MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.SpecialName | MethodAttributes.NewSlot |
MethodAttributes.Virtual;
// Implement the getter
MethodBuilder getterMethod = typeBuilder.DefineMethod("get_Interceptor", attributes,
CallingConventions.HasThis, typeof (IInterceptor),
new Type[0]);
getterMethod.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);
ILGenerator IL = getterMethod.GetILGenerator();
// This is equivalent to:
// get { return __interceptor;
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldfld, field);
IL.Emit(OpCodes.Ret);
// Implement the setter
MethodBuilder setterMethod = typeBuilder.DefineMethod("set_Interceptor", attributes,
CallingConventions.HasThis, typeof (void),
new Type[] {typeof (IInterceptor)});
setterMethod.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);
IL = setterMethod.GetILGenerator();
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldarg_1);
IL.Emit(OpCodes.Stfld, field);
IL.Emit(OpCodes.Ret);
MethodInfo originalSetter = typeof (IProxy).GetMethod("set_Interceptor");
MethodInfo originalGetter = typeof (IProxy).GetMethod("get_Interceptor");
typeBuilder.DefineMethodOverride(setterMethod, originalSetter);
typeBuilder.DefineMethodOverride(getterMethod, originalGetter);
}
}
}