-
Notifications
You must be signed in to change notification settings - Fork 10
/
DefaultProxyMethodBuilder.cs
67 lines (53 loc) · 2.16 KB
/
DefaultProxyMethodBuilder.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
namespace LinFu.DynamicProxy
{
internal class DefaultyProxyMethodBuilder : IProxyMethodBuilder
{
private IMethodBodyEmitter _emitter;
public DefaultyProxyMethodBuilder() : this(new DefaultMethodEmitter())
{
}
public DefaultyProxyMethodBuilder(IMethodBodyEmitter emitter)
{
_emitter = emitter;
}
public IMethodBodyEmitter MethodBodyEmitter
{
get { return _emitter; }
set { _emitter = value; }
}
#region IProxyMethodBuilder Members
public void CreateProxiedMethod(FieldInfo field, MethodInfo method, TypeBuilder typeBuilder)
{
ParameterInfo[] parameters = method.GetParameters();
List<Type> parameterTypes = new List<Type>();
foreach (ParameterInfo param in parameters)
{
parameterTypes.Add(param.ParameterType);
}
MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.Virtual;
MethodBuilder methodBuilder = typeBuilder.DefineMethod(method.Name, methodAttributes,
CallingConventions.HasThis, method.ReturnType,
parameterTypes.ToArray());
Type[] typeArgs = method.GetGenericArguments();
if (typeArgs != null && typeArgs.Length > 0)
{
List<string> typeNames = new List<string>();
for (int index = 0; index < typeArgs.Length; index++)
{
typeNames.Add(string.Format("T{0}", index));
}
methodBuilder.DefineGenericParameters(typeNames.ToArray());
}
ILGenerator IL = methodBuilder.GetILGenerator();
Debug.Assert(_emitter != null);
_emitter.EmitMethodBody(IL, method, field);
}
#endregion
}
}