1
- using System ;
2
- using System . Reflection ;
3
1
using System . Reflection . Emit ;
4
2
using System . Collections . Generic ;
5
- using NHibernate . Linq ;
6
- using NHibernate . Util ;
7
3
8
4
namespace NHibernate . Bytecode
9
5
{
10
- public class EmitUtil
6
+ public static class EmitUtil
11
7
{
12
- private EmitUtil ( )
13
- {
14
- }
15
-
16
- /// <summary>
17
- /// Emits an <c>ldc.i4</c> opcode using the fastest available opcode choice.
18
- /// </summary>
19
- public static void EmitFastInt ( ILGenerator il , int value )
20
- {
21
- switch ( value )
22
- {
23
- case - 1 :
24
- il . Emit ( OpCodes . Ldc_I4_M1 ) ;
25
- return ;
26
- case 0 :
27
- il . Emit ( OpCodes . Ldc_I4_0 ) ;
28
- return ;
29
- case 1 :
30
- il . Emit ( OpCodes . Ldc_I4_1 ) ;
31
- return ;
32
- case 2 :
33
- il . Emit ( OpCodes . Ldc_I4_2 ) ;
34
- return ;
35
- case 3 :
36
- il . Emit ( OpCodes . Ldc_I4_3 ) ;
37
- return ;
38
- case 4 :
39
- il . Emit ( OpCodes . Ldc_I4_4 ) ;
40
- return ;
41
- case 5 :
42
- il . Emit ( OpCodes . Ldc_I4_5 ) ;
43
- return ;
44
- case 6 :
45
- il . Emit ( OpCodes . Ldc_I4_6 ) ;
46
- return ;
47
- case 7 :
48
- il . Emit ( OpCodes . Ldc_I4_7 ) ;
49
- return ;
50
- case 8 :
51
- il . Emit ( OpCodes . Ldc_I4_8 ) ;
52
- return ;
53
- }
54
-
55
- if ( value > - 129 && value < 128 )
56
- {
57
- il . Emit ( OpCodes . Ldc_I4_S , ( SByte ) value ) ;
58
- }
59
- else
60
- {
61
- il . Emit ( OpCodes . Ldc_I4 , value ) ;
62
- }
63
- }
64
-
65
8
public static void EmitBoxIfNeeded ( ILGenerator il , System . Type type )
66
9
{
67
10
if ( type . IsValueType )
@@ -70,29 +13,25 @@ public static void EmitBoxIfNeeded(ILGenerator il, System.Type type)
70
13
}
71
14
}
72
15
73
- private static Dictionary < System . Type , OpCode > typeToOpcode ;
16
+ private static readonly Dictionary < System . Type , OpCode > typeToOpcode ;
74
17
75
18
static EmitUtil ( )
76
19
{
77
- typeToOpcode = new Dictionary < System . Type , OpCode > ( 12 ) ;
78
-
79
- typeToOpcode [ typeof ( bool ) ] = OpCodes . Ldind_I1 ;
80
- typeToOpcode [ typeof ( sbyte ) ] = OpCodes . Ldind_I1 ;
81
- typeToOpcode [ typeof ( byte ) ] = OpCodes . Ldind_U1 ;
82
-
83
- typeToOpcode [ typeof ( char ) ] = OpCodes . Ldind_U2 ;
84
- typeToOpcode [ typeof ( short ) ] = OpCodes . Ldind_I2 ;
85
- typeToOpcode [ typeof ( ushort ) ] = OpCodes . Ldind_U2 ;
86
-
87
- typeToOpcode [ typeof ( int ) ] = OpCodes . Ldind_I4 ;
88
- typeToOpcode [ typeof ( uint ) ] = OpCodes . Ldind_U4 ;
89
-
90
- typeToOpcode [ typeof ( long ) ] = OpCodes . Ldind_I8 ;
91
- typeToOpcode [ typeof ( ulong ) ] = OpCodes . Ldind_I8 ;
92
-
93
- typeToOpcode [ typeof ( float ) ] = OpCodes . Ldind_R4 ;
94
-
95
- typeToOpcode [ typeof ( double ) ] = OpCodes . Ldind_R8 ;
20
+ typeToOpcode = new Dictionary < System . Type , OpCode > ( 12 )
21
+ {
22
+ [ typeof ( bool ) ] = OpCodes . Ldind_I1 ,
23
+ [ typeof ( sbyte ) ] = OpCodes . Ldind_I1 ,
24
+ [ typeof ( byte ) ] = OpCodes . Ldind_U1 ,
25
+ [ typeof ( char ) ] = OpCodes . Ldind_U2 ,
26
+ [ typeof ( short ) ] = OpCodes . Ldind_I2 ,
27
+ [ typeof ( ushort ) ] = OpCodes . Ldind_U2 ,
28
+ [ typeof ( int ) ] = OpCodes . Ldind_I4 ,
29
+ [ typeof ( uint ) ] = OpCodes . Ldind_U4 ,
30
+ [ typeof ( long ) ] = OpCodes . Ldind_I8 ,
31
+ [ typeof ( ulong ) ] = OpCodes . Ldind_I8 ,
32
+ [ typeof ( float ) ] = OpCodes . Ldind_R4 ,
33
+ [ typeof ( double ) ] = OpCodes . Ldind_R8
34
+ } ;
96
35
}
97
36
98
37
/// <summary>
@@ -146,66 +85,5 @@ public static void PreparePropertyForSet(ILGenerator il, System.Type propertyTyp
146
85
}
147
86
}
148
87
}
149
-
150
- /// <summary>
151
- /// Defines a new delegate type.
152
- /// </summary>
153
- public static System . Type DefineDelegateType (
154
- string fullTypeName ,
155
- ModuleBuilder moduleBuilder ,
156
- System . Type returnType ,
157
- System . Type [ ] parameterTypes )
158
- {
159
- TypeBuilder delegateBuilder =
160
- moduleBuilder . DefineType (
161
- fullTypeName ,
162
- TypeAttributes . Class | TypeAttributes . Public | TypeAttributes . Sealed | TypeAttributes . AnsiClass |
163
- TypeAttributes . AutoClass , typeof ( MulticastDelegate ) ) ;
164
-
165
- // Define a special constructor
166
- ConstructorBuilder constructorBuilder =
167
- delegateBuilder . DefineConstructor (
168
- MethodAttributes . RTSpecialName | MethodAttributes . HideBySig | MethodAttributes . Public ,
169
- CallingConventions . Standard , new System . Type [ ] { typeof ( object ) , typeof ( IntPtr ) } ) ;
170
-
171
- constructorBuilder . SetImplementationFlags (
172
- MethodImplAttributes . Runtime | MethodImplAttributes . Managed ) ;
173
-
174
- // Define the Invoke method for the delegate
175
-
176
- MethodBuilder methodBuilder = delegateBuilder . DefineMethod ( "Invoke" ,
177
- MethodAttributes . Public | MethodAttributes . HideBySig
178
- | MethodAttributes . NewSlot | MethodAttributes . Virtual ,
179
- returnType , parameterTypes ) ;
180
-
181
- methodBuilder . SetImplementationFlags (
182
- MethodImplAttributes . Runtime | MethodImplAttributes . Managed ) ;
183
-
184
- return delegateBuilder . CreateType ( ) ;
185
- }
186
-
187
- public static void EmitLoadType ( ILGenerator il , System . Type type )
188
- {
189
- il . Emit ( OpCodes . Ldtoken , type ) ;
190
- il . Emit ( OpCodes . Call , ReflectionCache . TypeMethods . GetTypeFromHandle ) ;
191
- }
192
-
193
- public static void EmitLoadMethodInfo ( ILGenerator il , MethodInfo methodInfo )
194
- {
195
- il . Emit ( OpCodes . Ldtoken , methodInfo ) ;
196
- il . Emit ( OpCodes . Call , ReflectionCache . MethodBaseMethods . GetMethodFromHandle ) ;
197
- il . Emit ( OpCodes . Castclass , typeof ( MethodInfo ) ) ;
198
- }
199
-
200
- private static readonly MethodInfo CreateDelegate = ReflectHelper . GetMethod (
201
- ( ) => Delegate . CreateDelegate ( null , null ) ) ;
202
-
203
- public static void EmitCreateDelegateInstance ( ILGenerator il , System . Type delegateType , MethodInfo methodInfo )
204
- {
205
- EmitLoadType ( il , delegateType ) ;
206
- EmitLoadMethodInfo ( il , methodInfo ) ;
207
- il . EmitCall ( OpCodes . Call , CreateDelegate , null ) ;
208
- il . Emit ( OpCodes . Castclass , delegateType ) ;
209
- }
210
88
}
211
89
}
0 commit comments