forked from philiplaureano/LinFu.DynamicProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultArgumentHandler.cs
91 lines (73 loc) · 3.1 KB
/
DefaultArgumentHandler.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace LinFu.DynamicProxy
{
internal class DefaultArgumentHandler : IArgumentHandler
{
#region IArgumentHandler Members
public void PushArguments(ParameterInfo[] parameters, ILGenerator IL, bool isStatic)
{
int parameterCount = parameters == null ? 0 : parameters.Length;
// object[] args = new object[size];
IL.Emit(OpCodes.Ldc_I4, parameterCount);
IL.Emit(OpCodes.Newarr, typeof (object));
IL.Emit(OpCodes.Stloc_S, 0);
if (parameterCount == 0)
{
IL.Emit(OpCodes.Ldloc_S, 0);
return;
}
// Populate the object array with the list of arguments
int index = 0;
int argumentPosition = 1;
foreach (ParameterInfo param in parameters)
{
Type parameterType = param.ParameterType.IsByRef ? param.ParameterType.GetElementType() : param.ParameterType;
// args[N] = argumentN (pseudocode)
IL.Emit(OpCodes.Ldloc_S, 0);
IL.Emit(OpCodes.Ldc_I4, index);
// Zero out the [out] parameters
if (param.IsOut)
{
IL.Emit(OpCodes.Ldnull);
IL.Emit(OpCodes.Stelem_Ref);
argumentPosition++;
index++;
continue;
}
IL.Emit(OpCodes.Ldarg, argumentPosition);
bool isGeneric = parameterType.IsGenericParameter;
if (param.ParameterType.IsByRef)
{
var typeName = param.ParameterType.Name;
var ldindInstruction = OpCodes.Ldind_Ref;
var ldindMap = new Dictionary<string, OpCode>();
ldindMap["Bool&"] = OpCodes.Ldind_I1;
ldindMap["Int8&"] = OpCodes.Ldind_I1;
ldindMap["Uint8&"] = OpCodes.Ldind_I1;
ldindMap["Int16&"] = OpCodes.Ldind_I2;
ldindMap["Uint16&"] = OpCodes.Ldind_I2;
ldindMap["Uint32&"] = OpCodes.Ldind_I4;
ldindMap["Int32&"] = OpCodes.Ldind_I4;
ldindMap["IntPtr"] = OpCodes.Ldind_I4;
ldindMap["Uint64&"] = OpCodes.Ldind_I8;
ldindMap["Int64&"] = OpCodes.Ldind_I8;
ldindMap["Float32&"] = OpCodes.Ldind_R4;
ldindMap["Float64&"] = OpCodes.Ldind_R8;
if (ldindMap.ContainsKey(typeName))
ldindInstruction = ldindMap[typeName];
IL.Emit(ldindInstruction);
}
if (parameterType.IsValueType || parameterType.IsByRef || isGeneric)
IL.Emit(OpCodes.Box, parameterType);
IL.Emit(OpCodes.Stelem_Ref);
index++;
argumentPosition++;
}
IL.Emit(OpCodes.Ldloc_S, 0);
}
#endregion
}
}