A
download runtimehandles.cs
Language: C#
Copyright: (c) 2006 Microsoft Corporation. All rights reserved.
LOC: 1071
Project Info
Rotor IL Logger(rotorillogger)
Server: CodePlex1
Type: svn
...ger\svn\clr\src\bcl\system\
   __filters.cs
   __hresults.cs
   _localdatastore.cs
   _localdatastoremgr.cs
   ...ssviolationexception.cs
   activationarguments.cs
   activator.cs
   appdomain.cs
   appdomainattributes.cs
   appdomainmanager.cs
   appdomainsetup.cs
   ...ainunloadedexception.cs
   applicationactivator.cs
   applicationexception.cs
   applicationid.cs
   argiterator.cs
   argumentexception.cs
   argumentnullexception.cs
   ...toutofrangeexception.cs
   arithmeticexception.cs
   array.cs
   arraysegment.cs
   ...ypemismatchexception.cs
   asynccallback.cs
   attribute.cs
   attributetargets.cs
   attributeusageattribute.cs
   badimageformatexception.cs
   bcldebug.cs
   bitconverter.cs
   boolean.cs
   buffer.cs
   byte.cs
   ...adappdomainexception.cs
   cfgparser.cs
   char.cs
   charenumerator.cs
   clscompliantattribute.cs
   cominterfaces.cs
   compatibilityflags.cs
   ...usmethodframegeneric.cs
   console.cs
   consolecanceleventargs.cs
   consolespecialkey.cs
   contextboundobject.cs
   contextmarshalexception.cs
   contextstaticattribute.cs
   convert.cs
   currency.cs
   currenttimezone.cs
   datamisalignedexception.cs
   datetime.cs
   datetimekind.cs
   dayofweek.cs
   dbnull.cs
   decimal.cs
   defaultbinder.cs
   delegate.cs
   ...eserializationholder.cs
   dividebyzeroexception.cs
   dllnotfoundexception.cs
   double.cs
   ...ewaitobjectexception.cs
   empty.cs
   ...intnotfoundexception.cs
   enum.cs
   environment.cs
   eventargs.cs
   eventhandler.cs
   exception.cs
   ...utionengineexception.cs
   fieldaccessexception.cs
   flagsattribute.cs
   formatexception.cs
   gc.cs
   guid.cs
   iappdomain.cs
   iappdomainsetup.cs
   iasyncresult.cs
   icloneable.cs
   icomparable.cs
   iconvertible.cs
   icustomformatter.cs
   idisposable.cs
   iequatable.cs
   iformatprovider.cs
   iformattable.cs
   ...xoutofrangeexception.cs
   ...cientmemoryexception.cs
   int16.cs
   int32.cs
   int64.cs
   internal.cs
   intptr.cs
   invalidcastexception.cs
   ...idoperationexception.cs
   invalidprogramexception.cs
   iserviceobjectprovider.cs
   marshalbyrefobject.cs
   math.cs
   mda.cs
   memberaccessexception.cs
   methodaccessexception.cs
   midpointrounding.cs
   missingfieldexception.cs
   missingmemberexception.cs
   missingmethodexception.cs
   multicastdelegate.cs
   ...otsupportedexception.cs
   ndirectmethodframeex.cs
   nonserializedattribute.cs
   ...initenumberexception.cs
   notimplementedexception.cs
   notsupportedexception.cs
   nullable.cs
   nullreferenceexception.cs
   number.cs
   object.cs
   objectdisposedexception.cs
   obsoleteattribute.cs
   oleautbinder.cs
   operatingsystem.cs
   ...ioncanceledexception.cs
   outofmemoryexception.cs
   overflowexception.cs
   paramarrayattribute.cs
   parsenumbers.cs
   platformid.cs
   ...otsupportedexception.cs
   random.cs
   rankexception.cs
   resid.cs
   rttype.cs
   runtimeargumenthandle.cs
   runtimehandles.cs
   sbyte.cs
   serializableattribute.cs
   sharedstatics.cs
   single.cs
   stackoverflowexception.cs
   string.cs
   stringcomparer.cs
   stringcomparison.cs
   stringfreezingattribute.cs
   systemexception.cs
   threadattributes.cs
   threadstaticattribute.cs
   throwhelper.cs
   timeoutexception.cs
   timespan.cs
   timezone.cs
   type.cs
   typecode.cs
   typedreference.cs
   ...tializationexception.cs
   typeloadexception.cs
   typeunloadedexception.cs
   uint16.cs
   uint32.cs
   uint64.cs
   uintptr.cs
   ...rizedaccessexception.cs
   ...edexceptioneventargs.cs
   ...xceptioneventhandler.cs
   ...yserializationholder.cs
   unsafecharbuffer.cs
   valuetype.cs
   variant.cs
   version.cs
   void.cs
   weakreference.cs
   ...gnorememberattribute.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
// ==++==
// 
//   
//    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
//   
//    The use and distribution terms for this software are contained in the file
//    named license.txt, which can be found in the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by the
//    terms of this license.
//   
//    You must not remove this notice, or any other, from this software.
//   
// 
// ==--==

#define DEBUG_PTRS

namespace System 
{
    using System;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Runtime.ConstrainedExecution;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Globalization;
    using Microsoft.Win32.SafeHandles;
    using StackCrawlMark = System.Threading.StackCrawlMark;
    
    internal unsafe struct MethodDescChunkHandle
    {
        private IntPtr m_ptr;

        internal MethodDescChunkHandle(void* ptr)
        {
            m_ptr = new IntPtr(ptr);
        }

        internal bool IsNullHandle() 
        { 
            return m_ptr.ToPointer() == null; 
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetNextMethodDescChunk();
        internal MethodDescChunkHandle GetNextMethodDescChunk()
        {
            return new MethodDescChunkHandle(_GetNextMethodDescChunk());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetMethodAt(int index);
        internal RuntimeMethodHandle GetMethodAt(int index)
        {
            return new RuntimeMethodHandle(_GetMethodAt(index));
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetMethodCount();
    }

    internal struct FastArrayHandle
    {
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal static extern void SetValueAt(Array target, int index, object element);
/*        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]            
        internal static extern void CopyArray (Array soruce, Array destination);
*/        
    }

    [Serializable()]
    [System.Runtime.InteropServices.ComVisible(true)]
    public unsafe struct RuntimeTypeHandle : ISerializable
    {
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool IsInstanceOfType(Object o);

        internal unsafe static IntPtr GetTypeHelper(IntPtr th, IntPtr pGenericArgs, int cGenericArgs, IntPtr pModifiers, int cModifiers)
        {
            RuntimeTypeHandle typeHandle = new RuntimeTypeHandle(th.ToPointer());
            Type type = typeHandle.GetRuntimeType();
            if (type == null)
                return th;

            if (cGenericArgs > 0)
            {
                Type[] genericArgs = new Type[cGenericArgs];
                void** arGenericArgs = (void**)pGenericArgs.ToPointer();
                for (int i = 0; i < genericArgs.Length; i++)
                {
                    RuntimeTypeHandle genericArg = new RuntimeTypeHandle((void*)Marshal.ReadIntPtr((IntPtr)arGenericArgs, i * sizeof(void*)));
                    genericArgs[i] = Type.GetTypeFromHandle(genericArg);
                    
                    if (genericArgs[i] == null)
                        return (IntPtr)0;
                }

                type = type.MakeGenericType(genericArgs);
            }

            if (cModifiers > 0)
            {
                int* arModifiers = (int*)pModifiers.ToPointer();
                for(int i = 0; i < cModifiers; i++)
                {
                    if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.Ptr)
                        type = type.MakePointerType();
                    
                    else if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.ByRef)
                        type = type.MakeByRefType();

                    else if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.SzArray)
                        type = type.MakeArrayType();

                    else
                        type = type.MakeArrayType(Marshal.ReadInt32((IntPtr)arModifiers, ++i * sizeof(int)));
                }
            }
            
            return type.GetTypeHandleInternal().Value;
        }

        public static bool operator ==(RuntimeTypeHandle left, object right) { return left.Equals(right); }
        
        public static bool operator ==(object left, RuntimeTypeHandle right) { return right.Equals(left); }
        
        public static bool operator !=(RuntimeTypeHandle left, object right) { return !left.Equals(right); }

        public static bool operator !=(object left, RuntimeTypeHandle right) { return !right.Equals(left); }
/*
        internal new Type GetType() 
        { 
            Console.WriteLine("RuntimeTypeHandle.GetType() called"); 
            throw new Exception("RuntimeTypeHandle.GetType() called. Chances are you want to call GetRuntimeType()");
            return base.GetType(); 
        }
*/
        private const int MAX_CLASS_NAME = 1024;
        internal static readonly RuntimeTypeHandle EmptyHandle = new RuntimeTypeHandle(null);

        // this is the TypeHandle/MethodTable for the type
        private IntPtr m_ptr;

        public override int GetHashCode()
        {
            return m_ptr.GetHashCode();
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public override bool Equals(object obj)
        {
            if(!(obj is RuntimeTypeHandle))
                return false;

            RuntimeTypeHandle handle =(RuntimeTypeHandle)obj;
            return handle.m_ptr == m_ptr;
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public bool Equals(RuntimeTypeHandle handle)
        {
            return handle.m_ptr == m_ptr;
        }

        public IntPtr Value { get { return m_ptr; } }

        internal RuntimeTypeHandle(void* rth) 
        {
            m_ptr = new IntPtr(rth);
        }

        internal bool IsNullHandle() 
        { 
            return m_ptr.ToPointer() == null; 
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
	    internal static extern Object CreateInstance(RuntimeType type, bool publicOnly, bool noCheck, ref bool canBeCached, ref RuntimeMethodHandle ctor, ref bool bNeedSecurityCheck);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
	    internal extern Object CreateCaInstance(RuntimeMethodHandle ctor);

	    [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern Object Allocate();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern Object CreateInstanceForAnotherGenericParameter(Type genericParameter);
        
        internal RuntimeType GetRuntimeType()
        {            
            if(!IsNullHandle())
                return(System.RuntimeType)Type.GetTypeFromHandle(this);
            else 
                return null;
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern CorElementType GetCorElementType(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetAssemblyHandle();
        internal AssemblyHandle GetAssemblyHandle()
        {
            return new AssemblyHandle(_GetAssemblyHandle());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        private extern void* _GetModuleHandle();

        [CLSCompliant(false)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public ModuleHandle GetModuleHandle()
        {
            return new ModuleHandle(_GetModuleHandle());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetBaseTypeHandle();
        internal RuntimeTypeHandle GetBaseTypeHandle()
        {
            return new RuntimeTypeHandle(_GetBaseTypeHandle());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern TypeAttributes GetAttributes(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetElementType();
        internal RuntimeTypeHandle GetElementType()
        {
            return new RuntimeTypeHandle(_GetElementType());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern RuntimeTypeHandle GetCanonicalHandle();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetArrayRank(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetToken(); 
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetMethodAt(int slot);
        internal RuntimeMethodHandle GetMethodAt(int slot)
        {
            return new RuntimeMethodHandle(_GetMethodAt(slot));
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetMethodDescChunk();
        internal MethodDescChunkHandle GetMethodDescChunk()
        {
            return new MethodDescChunkHandle(_GetMethodDescChunk());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool GetFields(int** result, int* count);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern RuntimeTypeHandle[] GetInterfaces();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern RuntimeTypeHandle[] GetConstraints();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern IntPtr GetGCHandle(GCHandleType type); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern void FreeGCHandle(IntPtr handle); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetMethodFromToken(int tkMethodDef);
        internal RuntimeMethodHandle GetMethodFromToken(int tkMethodDef)
        {
            return new RuntimeMethodHandle(_GetMethodFromToken(tkMethodDef));
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetNumVtableSlots(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetInterfaceMethodSlots();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern int GetFirstSlotForInterface(IntPtr interfaceHandle);
        internal int GetFirstSlotForInterface(RuntimeTypeHandle interfaceHandle)
        {
            return GetFirstSlotForInterface(interfaceHandle.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern int GetInterfaceMethodImplementationSlot(IntPtr interfaceHandle, IntPtr interfaceMethodHandle);
        internal int GetInterfaceMethodImplementationSlot(RuntimeTypeHandle interfaceHandle, RuntimeMethodHandle interfaceMethodHandle)
        {
            return GetInterfaceMethodImplementationSlot(interfaceHandle.Value, interfaceMethodHandle.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool IsComObject(bool isGenericCOM); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool IsContextful(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool IsVisible(); 
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool _IsVisibleFromModule(IntPtr module); 
        internal bool IsVisibleFromModule(ModuleHandle module)
        {
            return _IsVisibleFromModule((IntPtr)module.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool HasProxyAttribute(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern string ConstructName(bool nameSpace, bool fullInst, bool assembly);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetUtf8Name();

        internal Utf8String GetUtf8Name()
        {
            return new Utf8String(_GetUtf8Name());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern bool CanCastTo(IntPtr target);
        internal bool CanCastTo(RuntimeTypeHandle target)
        {
            return CanCastTo(target.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern RuntimeTypeHandle GetDeclaringType();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetDeclaringMethod();
        internal RuntimeMethodHandle GetDeclaringMethod()
        {
            return new RuntimeMethodHandle(_GetDeclaringMethod());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetDefaultConstructor();        
        internal RuntimeMethodHandle GetDefaultConstructor()       
        {
            return new RuntimeMethodHandle(_GetDefaultConstructor());
        }
       
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool SupportsInterface(object target);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern static void* _GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref StackCrawlMark stackMark, bool loadTypeFromPartialName);
        internal static RuntimeTypeHandle GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref StackCrawlMark stackMark)
        {
            if (name == null || name.Length == 0)
            {
                if (throwOnError)
                    throw new TypeLoadException(Environment.GetResourceString("Arg_TypeLoadNullStr"));

                return new RuntimeTypeHandle();
            }
            
            return new RuntimeTypeHandle(_GetTypeByName(name, throwOnError, ignoreCase, reflectionOnly, ref stackMark, false));
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern static void* _GetTypeByNameUsingCARules(string name, IntPtr scope);
        internal static Type GetTypeByNameUsingCARules(string name, Module scope)
        {
            if (name == null || name.Length == 0)
                throw new ArgumentException(); 

            return new RuntimeTypeHandle(_GetTypeByNameUsingCARules(name, (IntPtr)scope.GetModuleHandle().Value)).GetRuntimeType();
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern RuntimeTypeHandle[] GetInstantiation(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _Instantiate(RuntimeTypeHandle[] inst);
        internal RuntimeTypeHandle Instantiate(RuntimeTypeHandle[] inst)
        {
            return new RuntimeTypeHandle(_Instantiate(inst));
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _MakeArray(int rank);
        internal RuntimeTypeHandle MakeArray(int rank)
        {
            return new RuntimeTypeHandle(_MakeArray(rank));
        }
/*
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern void* _GetCallersType(ref StackCrawlMark stackMark);
        internal static RuntimeTypeHandle GetCallersType(ref StackCrawlMark stackMark)
        {
            return new RuntimeTypeHandle(_GetCallersType(ref stackMark));
        }
*/
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _MakeSZArray();
        internal RuntimeTypeHandle MakeSZArray()
        {
            return new RuntimeTypeHandle(_MakeSZArray());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _MakeByRef();
        internal RuntimeTypeHandle MakeByRef()
        {
            return new RuntimeTypeHandle(_MakeByRef());
        }
       
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _MakePointer();
        internal RuntimeTypeHandle MakePointer()
        {
            return new RuntimeTypeHandle(_MakePointer());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool HasInstantiation();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetGenericTypeDefinition();
        internal RuntimeTypeHandle GetGenericTypeDefinition()
        {
            return new RuntimeTypeHandle(_GetGenericTypeDefinition());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool IsGenericTypeDefinition(); 
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool IsGenericVariable(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetGenericVariableIndex(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool ContainsGenericVariables();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern bool SatisfiesConstraints(RuntimeTypeHandle[] typeContext, RuntimeTypeHandle[] methodContext, RuntimeTypeHandle toType);
        
        private RuntimeTypeHandle(SerializationInfo info, StreamingContext context)
        {
            if(info == null) 
                throw new ArgumentNullException("info");

            Type m = (RuntimeType)info.GetValue("TypeObj", typeof(RuntimeType));

            m_ptr = m.TypeHandle.Value;

            if(m_ptr.ToPointer() == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context) 
        {
            if(info == null) 
                throw new ArgumentNullException("info");

            if(m_ptr.ToPointer() == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFieldState")); 

            RuntimeType type = (RuntimeType)Type.GetTypeFromHandle(this); 

            info.AddValue("TypeObj", type, typeof(RuntimeType));
        }
    }                                       

    [Serializable()]
[System.Runtime.InteropServices.ComVisible(true)]
    public unsafe struct RuntimeMethodHandle : ISerializable
    {
        internal static RuntimeMethodHandle EmptyHandle { get { return new RuntimeMethodHandle(null); } }
        
        private IntPtr m_ptr;
        
        internal RuntimeMethodHandle(void* pMethod) 
        {
            m_ptr = new IntPtr(pMethod);
        }
        
        internal RuntimeMethodHandle(IntPtr pMethod) 
        {
            m_ptr = pMethod;
        }
        
        // ISerializable interface
        private RuntimeMethodHandle(SerializationInfo info, StreamingContext context)
        {
            if(info == null)
                throw new ArgumentNullException("info");
            
            MethodInfo m =(RuntimeMethodInfo)info.GetValue("MethodObj", typeof(RuntimeMethodInfo));

            m_ptr = m.MethodHandle.Value;
            
            if(m_ptr.ToPointer() == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context) 
        {
            if (info == null) 
                throw new ArgumentNullException("info");
            
            if (m_ptr.ToPointer() == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFieldState"));
            
            RuntimeMethodInfo methodInfo = (RuntimeMethodInfo)RuntimeType.GetMethodBase(this); 
            
            info.AddValue("MethodObj", methodInfo, typeof(RuntimeMethodInfo));
        }       
        
        public IntPtr Value { get {  return m_ptr; } }

        public override int GetHashCode()
        {
            return m_ptr.GetHashCode();
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public override bool Equals(object obj)
        {
            if (!(obj is RuntimeMethodHandle))
                return false;

            RuntimeMethodHandle handle = (RuntimeMethodHandle)obj;

            return handle.m_ptr == m_ptr;
        }

        public static bool operator ==(RuntimeMethodHandle left, RuntimeMethodHandle right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(RuntimeMethodHandle left, RuntimeMethodHandle right)
        {
            return !left.Equals(right);
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public bool Equals(RuntimeMethodHandle handle)
        {
            return handle.m_ptr == m_ptr;
        }

        internal bool IsNullHandle() 
        { 
            return m_ptr.ToPointer() == null; 
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        public extern IntPtr GetFunctionPointer();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private unsafe extern void _CheckLinktimeDemands(void* module, int metadataToken);
        internal void CheckLinktimeDemands(Module module, int metadataToken)
        {
            _CheckLinktimeDemands((void*)module.ModuleHandle.Value, metadataToken);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private unsafe extern bool _IsVisibleFromModule(void* source);
        internal bool IsVisibleFromModule(Module source)
        {
            return _IsVisibleFromModule(source.ModuleHandle.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private unsafe extern bool _IsVisibleFromType(IntPtr source);
        internal bool IsVisibleFromType(RuntimeTypeHandle source)
        {
            return _IsVisibleFromType(source.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern void* _GetCurrentMethod(ref StackCrawlMark stackMark);
        internal static RuntimeMethodHandle GetCurrentMethod(ref StackCrawlMark stackMark)
        {
            return new RuntimeMethodHandle(_GetCurrentMethod(ref stackMark));
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern MethodAttributes GetAttributes();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern MethodImplAttributes GetImplAttributes();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern string ConstructInstantiation();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern RuntimeTypeHandle GetDeclaringType();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetSlot();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern int GetMethodDef();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern string GetName();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern void* _GetUtf8Name();

        internal Utf8String GetUtf8Name()
        {
            return new Utf8String(_GetUtf8Name());
        }

        [DebuggerStepThroughAttribute]
        [Diagnostics.DebuggerHidden]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern object _InvokeMethodFast(
            object target, object[] arguments, ref SignatureStruct sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner);

        [DebuggerStepThroughAttribute]
        [Diagnostics.DebuggerHidden]
        internal object InvokeMethodFast(object target, object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
        {
              SignatureStruct _sig = sig.m_signature;
              object obj1 = _InvokeMethodFast(target, arguments, ref _sig, methodAttributes, typeOwner);
              sig.m_signature = _sig;
              return obj1;
        }
       
        [DebuggerStepThroughAttribute]
        [Diagnostics.DebuggerHidden]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern object _InvokeConstructor(object[] args, ref SignatureStruct signature, IntPtr declaringType);

        [DebuggerStepThroughAttribute]
        [Diagnostics.DebuggerHidden]
        internal object InvokeConstructor (object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
        {
            return _InvokeConstructor(args, ref signature, declaringType.Value);
        }

        [DebuggerStepThroughAttribute]
        [Diagnostics.DebuggerHidden]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern void _SerializationInvoke(
            Object target, ref SignatureStruct declaringTypeSig, SerializationInfo info, StreamingContext context);

        [DebuggerStepThroughAttribute]
        [Diagnostics.DebuggerHidden]
        internal void SerializationInvoke(
            Object target, SignatureStruct declaringTypeSig, SerializationInfo info, StreamingContext context)
        {
            _SerializationInvoke(target, ref declaringTypeSig, info, context);
        }


        [MethodImplAttribute (MethodImplOptions.InternalCall)]        
        internal extern bool IsILStub();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern RuntimeTypeHandle[] GetMethodInstantiation(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern bool HasMethodInstantiation(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern RuntimeMethodHandle GetInstantiatingStub(RuntimeTypeHandle declaringTypeHandle, RuntimeTypeHandle[] methodInstantiation);
        
        internal RuntimeMethodHandle GetInstantiatingStubIfNeeded(RuntimeTypeHandle declaringTypeHandle)
        {
            return GetInstantiatingStub(declaringTypeHandle, null);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern RuntimeMethodHandle GetMethodFromCanonical(RuntimeTypeHandle declaringTypeHandle);
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern bool IsGenericMethodDefinition(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern void* _GetTypicalMethodDefinition(); 
        internal RuntimeMethodHandle GetTypicalMethodDefinition()
        {
            return new RuntimeMethodHandle(_GetTypicalMethodDefinition());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern void* _StripMethodInstantiation(); 
        internal RuntimeMethodHandle StripMethodInstantiation()
        {
            return new RuntimeMethodHandle(_StripMethodInstantiation());
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern bool IsDynamicMethod();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern Resolver GetResolver(); 
    
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern void Destroy(); 

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern MethodBody _GetMethodBody(IntPtr declaringType);
        internal MethodBody GetMethodBody(RuntimeTypeHandle declaringType)
        {
            return _GetMethodBody(declaringType.Value);
        }

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern bool IsConstructor();
    }

    [Serializable()]
[System.Runtime.InteropServices.ComVisible(true)]
    public unsafe struct RuntimeFieldHandle : ISerializable
    {
        private IntPtr m_ptr;

        internal RuntimeFieldHandle(void* pFieldHandle) 
        {
            m_ptr = new IntPtr(pFieldHandle);
        }
        
        public IntPtr Value { get { return m_ptr; } }

        internal bool IsNullHandle() 
        { 
            return m_ptr.ToPointer() == null; 
        }

        public override  int GetHashCode()
        {
            return m_ptr.GetHashCode();
        }
        
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public override  bool Equals(object obj)
        {
            if (!(obj is RuntimeFieldHandle))
                return false;

            RuntimeFieldHandle handle = (RuntimeFieldHandle)obj;

            return handle.m_ptr == m_ptr;
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public unsafe bool Equals(RuntimeFieldHandle handle)
        {
            return handle.m_ptr == m_ptr;
        }

        public static bool operator ==(RuntimeFieldHandle left, RuntimeFieldHandle right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(RuntimeFieldHandle left, RuntimeFieldHandle right)
        {
            return !left.Equals(right);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern String GetName(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern unsafe void* _GetUtf8Name(); 

        internal unsafe Utf8String GetUtf8Name() { return new Utf8String(_GetUtf8Name()); }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern FieldAttributes GetAttributes(); 
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern RuntimeTypeHandle GetApproxDeclaringType();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern int GetToken(); 

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern Object GetValue(Object instance, RuntimeTypeHandle fieldType, RuntimeTypeHandle declaringType, ref bool domainInitialized);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern Object GetValueDirect(RuntimeTypeHandle fieldType, TypedReference obj, RuntimeTypeHandle contextType);
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern void SetValue(Object obj, Object value, RuntimeTypeHandle fieldType, FieldAttributes fieldAttr, RuntimeTypeHandle declaringType, ref bool domainInitialized);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern void SetValueDirect(RuntimeTypeHandle fieldType, TypedReference obj, Object value, RuntimeTypeHandle contextType);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern RuntimeFieldHandle GetStaticFieldForGenericType(RuntimeTypeHandle declaringType);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern bool AcquiresContextFromThis();

        // ISerializable interface
        private RuntimeFieldHandle(SerializationInfo info, StreamingContext context)
        {
            if(info==null)
                throw new ArgumentNullException("info");
            
            FieldInfo f =(RuntimeFieldInfo) info.GetValue("FieldObj", typeof(RuntimeFieldInfo));
            
            if( f == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));

            m_ptr = f.FieldHandle.Value;

            if (m_ptr.ToPointer() == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context) 
        {
            if (info == null) 
                throw new ArgumentNullException("info");
            
            if (m_ptr.ToPointer() == null)
                throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFieldState"));

            RuntimeFieldInfo fldInfo = (RuntimeFieldInfo)RuntimeType.GetFieldInfo(this); 
            
            info.AddValue("FieldObj",fldInfo, typeof(RuntimeFieldInfo));
        }
    }

    internal unsafe struct AssemblyHandle
    {        
        #region Private Data Members
        private IntPtr m_ptr;
        #endregion

        internal void* Value { get { return m_ptr.ToPointer(); } }

        #region Constructor
        internal AssemblyHandle(void* pAssembly)
        {
            m_ptr = new IntPtr(pAssembly);
        }
        #endregion

        #region Internal Members
                
        public override  int GetHashCode()
        {
            return m_ptr.GetHashCode();
        }

        public override  bool Equals(object obj)
        {
            if (!(obj is AssemblyHandle))
                return false;

            AssemblyHandle handle = (AssemblyHandle)obj;

            return handle.m_ptr == m_ptr;
        }

        public unsafe bool Equals(AssemblyHandle handle)
        {
            return handle.m_ptr == m_ptr;
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern Assembly GetAssembly();
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern void* _GetManifestModule();
        internal ModuleHandle GetManifestModule()
        {
            return new ModuleHandle(_GetManifestModule());
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern bool _AptcaCheck(IntPtr sourceAssembly);
        internal bool AptcaCheck(AssemblyHandle sourceAssembly)
        {
            return _AptcaCheck((IntPtr)sourceAssembly.Value);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern int GetToken();
        #endregion
    }

[System.Runtime.InteropServices.ComVisible(true)]
    public unsafe struct ModuleHandle
    {
        #region Public Static Members
        public static readonly ModuleHandle EmptyHandle = new ModuleHandle(null);
        #endregion

        #region Private Data Members
        private IntPtr m_ptr;
        #endregion
    
        #region Constructor
        internal ModuleHandle(void* pModule) 
        {
            m_ptr = new IntPtr(pModule);
        }
        #endregion

        #region Internal FCalls
        internal void* Value { get { return m_ptr.ToPointer(); } }

        internal bool IsNullHandle() 
        { 
            return m_ptr.ToPointer() == null; 
        }
        
        public override  int GetHashCode()
        {           
            return m_ptr.GetHashCode();
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public override bool Equals(object obj)
        {
            if (!(obj is ModuleHandle))
                return false;

            ModuleHandle handle = (ModuleHandle)obj;

            return handle.m_ptr == m_ptr;
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public unsafe bool Equals(ModuleHandle handle)
        {
            return handle.m_ptr == m_ptr;
        }

        public static bool operator ==(ModuleHandle left, ModuleHandle right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(ModuleHandle left, ModuleHandle right)
        {
            return !left.Equals(right);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal static extern RuntimeTypeHandle GetCallerType(ref StackCrawlMark stackMark);

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal static extern void* GetDynamicMethod(void* module, string name, byte[] sig, Resolver resolver);
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern int GetToken();

        internal static RuntimeTypeHandle[] CopyRuntimeTypeHandles(RuntimeTypeHandle[] inHandles)
        {
            if (inHandles == null || inHandles.Length == 0) return inHandles;
            RuntimeTypeHandle[] outHandles = new RuntimeTypeHandle[inHandles.Length];
            for (int i=0; i < inHandles.Length; i++)
            {
                outHandles[i] = inHandles[i];
            }
            return outHandles;
        }

        private void ValidateModulePointer()
        {
            // Make sure we have a valid Module to resolve against.
            if (IsNullHandle())
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullModuleHandle"));
        }

        // SQL-CLR LKG9 Compiler dependency
        public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken(int typeToken) { return ResolveTypeHandle(typeToken); }
        public RuntimeTypeHandle ResolveTypeHandle(int typeToken) { return ResolveTypeHandle(typeToken, null, null); }
        public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
        {
            ValidateModulePointer();
            
            // defensive copy to be sure array is not mutated from the outside during processing
            typeInstantiationContext = CopyRuntimeTypeHandles(typeInstantiationContext);
            methodInstantiationContext = CopyRuntimeTypeHandles(methodInstantiationContext);
            
            unsafe
            {
                if (typeInstantiationContext == null || typeInstantiationContext.Length == 0) 
                {
                    if (methodInstantiationContext == null || methodInstantiationContext.Length == 0) 
                    {
                        // no context
                        return ResolveType(typeToken, null, 0, null, 0);
                    }
                    // only method context available
                    int instCount = methodInstantiationContext.Length;
                    fixed (RuntimeTypeHandle* instArgs = methodInstantiationContext) {
                        return ResolveType(typeToken, null, 0, instArgs, instCount);
                    }
                }
                if (methodInstantiationContext == null || methodInstantiationContext.Length == 0) 
                {
                    // only type context available
                    int instCount = typeInstantiationContext.Length;
                    fixed (RuntimeTypeHandle* instArgs = typeInstantiationContext) {
                        return ResolveType(typeToken, instArgs, instCount, null, 0);
                    }
                }
                // pin both
                int typeInstCount = typeInstantiationContext.Length;
                int methodInstCount = methodInstantiationContext.Length;
                fixed (RuntimeTypeHandle* typeInstArgs = typeInstantiationContext, methodInstArgs = methodInstantiationContext) {
                    return ResolveType(typeToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount);
                }
            }
        }
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern unsafe RuntimeTypeHandle ResolveType(int typeToken, 
                                                            RuntimeTypeHandle* typeInstArgs, 
                                                            int typeInstCount,
                                                            RuntimeTypeHandle* methodInstArgs,
                                                            int methodInstCount);

        // SQL-CLR LKG9 Compiler dependency
        public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken(int methodToken) { return ResolveMethodHandle(methodToken); }
        public RuntimeMethodHandle ResolveMethodHandle(int methodToken) { return ResolveMethodHandle(methodToken, null, null); }
        public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
        {
            ValidateModulePointer();
            
            // defensive copy to be sure array is not mutated from the outside during processing
            typeInstantiationContext = CopyRuntimeTypeHandles(typeInstantiationContext);
            methodInstantiationContext = CopyRuntimeTypeHandles(methodInstantiationContext);
            
            unsafe
            {
                if (typeInstantiationContext == null || typeInstantiationContext.Length == 0) 
                {
                    if (methodInstantiationContext == null || methodInstantiationContext.Length == 0) 
                    {
                        // no context
                        return ResolveMethod(methodToken, null, 0, null, 0);
                    }
                    // only method context available
                    int instCount = methodInstantiationContext.Length;
                    fixed (RuntimeTypeHandle* instArgs = methodInstantiationContext) {
                        return ResolveMethod(methodToken, null, 0, instArgs, instCount);
                    }
                }
                if (methodInstantiationContext == null || methodInstantiationContext.Length == 0) 
                {
                    // only type context available
                    int instCount = typeInstantiationContext.Length;
                    fixed (RuntimeTypeHandle* instArgs = typeInstantiationContext) {
                        return ResolveMethod(methodToken, instArgs, instCount, null, 0);
                    }
                }
                // pin both
                int typeInstCount = typeInstantiationContext.Length;
                int methodInstCount = methodInstantiationContext.Length;
                fixed (RuntimeTypeHandle* typeInstArgs = typeInstantiationContext, methodInstArgs = methodInstantiationContext) {
                    return ResolveMethod(methodToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount);
                }
            }
        }
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private extern RuntimeMethodHandle ResolveMethod(int methodToken, 
                                                        RuntimeTypeHandle* typeInstArgs, 
                                                        int typeInstCount,
                                                        RuntimeTypeHandle* methodInstArgs,
                                                        int methodInstCount);

        // SQL-CLR LKG9 Compiler dependency
        public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken(int fieldToken) { return ResolveFieldHandle(fieldToken); }
        public RuntimeFieldHandle ResolveFieldHandle(int fieldToken) { return ResolveFieldHandle(fieldToken, null, null); }
        public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
        {
            ValidateModulePointer();
            
            // defensive copy to be sure array is not mutated from the outside during processing
            typeInstantiationContext = CopyRuntimeTypeHandles(typeInstantiationContext);
            methodInstantiationContext = CopyRuntimeTypeHandles(methodInstantiationContext);
            
            unsafe
            {
                if (typeInstantiationContext == null || typeInstantiationContext.Length == 0) 
                {
                    if (methodInstantiationContext == null || methodInstantiationContext.Length == 0) 
                    {
                        // no context
                        return ResolveField(fieldToken, null, 0, null, 0);
                    }
                    // only method context available
                    int instCount = methodInstantiationContext.Length;
                    fixed (RuntimeTypeHandle* instArgs = methodInstantiationContext) {
                        return ResolveField(fieldToken, null, 0, instArgs, instCount);
                    }
                }
                if (methodInstantiationContext == null || methodInstantiationContext.Length == 0) 
                {
                    // only type context available
                    int instCount = typeInstantiationContext.Length;
                    fixed (RuntimeTypeHandle* instArgs = typeInstantiationContext) {
                        return ResolveField(fieldToken, instArgs, instCount, null, 0);
                    }
                }
                // pin both
                int typeInstCount = typeInstantiationContext.Length;
                int methodInstCount = methodInstantiationContext.Length;
                fixed (RuntimeTypeHandle* typeInstArgs = typeInstantiationContext, methodInstArgs = methodInstantiationContext) {
                    return ResolveField(fieldToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount);
                }
            }
        }
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private extern RuntimeFieldHandle ResolveField(int fieldToken, 
                                                      RuntimeTypeHandle* typeInstArgs, 
                                                      int typeInstCount,
                                                      RuntimeTypeHandle* methodInstArgs,
                                                      int methodInstCount);
        
//        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
//        private extern void* _GetAssemblyHandle();
//        internal AssemblyHandle GetAssemblyHandle()
//        {
//            return new AssemblyHandle(_GetAssemblyHandle());
//        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern Module GetModule();

        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern void* _GetModuleTypeHandle();
        internal RuntimeTypeHandle GetModuleTypeHandle()
        {
            return new RuntimeTypeHandle(_GetModuleTypeHandle());
        }        
 
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern void _GetPEKind(out int peKind, out int machine);
   
        // making this internal, used by Module.GetPEKind
        internal void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
        {
            int _peKind;
            int _machine;

            _GetPEKind(out _peKind, out _machine);

            peKind = (PortableExecutableKinds)_peKind;
            machine = (ImageFileMachine)_machine;
        }
   
	[MethodImplAttribute(MethodImplOptions.InternalCall)]        
        internal extern int _GetMDStreamVersion();
        public int MDStreamVersion
        {
	    get {  return _GetMDStreamVersion(); }
        }
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        internal extern void * _GetMetadataImport(); 
        internal MetadataImport GetMetadataImport()
        {
            return new MetadataImport((IntPtr)_GetMetadataImport());
        }
        #endregion
    }


    internal unsafe class Signature
    {
        #region Implict Cast Operators
        public static implicit operator SignatureStruct(Signature pThis) { return pThis.m_signature; }
        #endregion

        #region Definitions
        internal enum MdSigCallingConvention : byte
        {
            Generics            = 0x10,
            HasThis             = 0x20,
            ExplicitThis        = 0x40,
            CallConvMask        = 0x0F,
            Default             = 0x00,
            C                   = 0x01,
            StdCall             = 0x02,
            ThisCall            = 0x03,
            FastCall            = 0x04,
            Vararg              = 0x05,
            Field               = 0x06,
            LocalSig            = 0x07,
            Property            = 0x08,
            Unmgd               = 0x09,
            GenericInst         = 0x0A,
            Max                 = 0x0B,
        }
        #endregion

        #region FCalls
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private static extern void _GetSignature(
            ref SignatureStruct signature, // This parameter must be passed as a pointer to the stack to prevent GC holes
            void* pCorSig, int cCorSig,
            IntPtr fieldHandle, IntPtr methodHandle, IntPtr declaringTypeHandle);
        private static void GetSignature(
            ref SignatureStruct signature, // This parameter must be passed as a pointer to the stack to prevent GC holes
            void* pCorSig, int cCorSig,
            RuntimeFieldHandle fieldHandle, RuntimeMethodHandle methodHandle, RuntimeTypeHandle declaringTypeHandle)
        {
            _GetSignature(ref signature, pCorSig, cCorSig, fieldHandle.Value, methodHandle.Value, declaringTypeHandle.Value);
        }

        internal static void GetSignatureForDynamicMethod(
            ref SignatureStruct signature, // This parameter must be passed as a pointer to the stack to prevent GC holes
            RuntimeMethodHandle methodHandle)
        {
            _GetSignature(ref signature, null, 0, (IntPtr)0, methodHandle.Value, (IntPtr)0);
        }

        [MethodImplAttribute(MethodImplOptions.InternalCall)]       
        private static extern void GetCustomModifiers(
            ref SignatureStruct signature, int parameter, 
            out RuntimeTypeHandle[] required, out RuntimeTypeHandle[] optional); 
        
        [MethodImplAttribute(MethodImplOptions.InternalCall)]        
        private static extern bool CompareSig(ref SignatureStruct left, ref SignatureStruct right); 
        #endregion

        #region Private Data Members
        internal SignatureStruct m_signature;
        #endregion

        #region Constructors
        public Signature (            
            RuntimeMethodHandle method,
            RuntimeTypeHandle[] arguments,
            RuntimeTypeHandle returnType,
            CallingConventions callingConvention)
        {
            SignatureStruct pinnedSignature = new SignatureStruct(
                method, arguments, returnType, callingConvention);
            GetSignatureForDynamicMethod(ref pinnedSignature, method);
            m_signature = pinnedSignature;
        }

        public Signature(RuntimeMethodHandle methodHandle, RuntimeTypeHandle declaringTypeHandle)
        {
            SignatureStruct pinnedSignature = new SignatureStruct();
            GetSignature(ref pinnedSignature, null, 0, new RuntimeFieldHandle(null), methodHandle, declaringTypeHandle);
            m_signature = pinnedSignature;
        }

        public Signature(RuntimeFieldHandle fieldHandle, RuntimeTypeHandle declaringTypeHandle)
        {
            SignatureStruct pinnedSignature = new SignatureStruct();
            GetSignature(ref pinnedSignature, null, 0, fieldHandle, new RuntimeMethodHandle(null), declaringTypeHandle);
            m_signature = pinnedSignature;
        }

        public Signature(void* pCorSig, int cCorSig, RuntimeTypeHandle declaringTypeHandle)
        {
            SignatureStruct pinnedSignature = new SignatureStruct();
            GetSignature(ref pinnedSignature, pCorSig, cCorSig, new RuntimeFieldHandle(null), new RuntimeMethodHandle(null), declaringTypeHandle);
            m_signature = pinnedSignature;
        }
        #endregion

        #region Internal Members
        internal CallingConventions CallingConvention { get { return m_signature.m_managedCallingConvention &(CallingConventions)0x000000FF; } }
        internal RuntimeTypeHandle[] Arguments { get { return m_signature.m_arguments; } }
        internal RuntimeTypeHandle ReturnTypeHandle { get { return m_signature.m_returnTypeORfieldType; } }
        internal RuntimeTypeHandle FieldTypeHandle { get { return m_signature.m_returnTypeORfieldType; } }
        internal bool DiffSigs(object o)
        {
            if(o is Signature)
            {              
                Signature sig = o as Signature; 
                SignatureStruct pinnedSig = sig;
                SignatureStruct pinnedThis = this;
                return CompareSig(ref pinnedThis, ref pinnedSig);
            }

            return false;
        }

        public Type[] GetCustomModifiers(int position, bool required)
        {
            RuntimeTypeHandle[] req = null, opt = null;

            SignatureStruct pinnedSig = this;
            GetCustomModifiers(ref pinnedSig, position, out req, out opt);

            Type[] custMod = new Type[required ? req.Length : opt.Length];

            if(required)
            {
                for(int i = 0; i < custMod.Length; i++)
                    custMod[i] = req[i].GetRuntimeType();
            }
            else
            {
                for(int i = 0; i < custMod.Length; i++)
                    custMod[i] = opt[i].GetRuntimeType();
            }

            return custMod;
        }
        #endregion
    }

    //
    // WARNING: SignatureStructs should not be share between methods! Call targets are cachded!
    //
    internal unsafe struct SignatureStruct
    {
        #region Constructor
        public SignatureStruct (
            RuntimeMethodHandle method,
            RuntimeTypeHandle[] arguments,
            RuntimeTypeHandle returnType,
            CallingConventions callingConvention)
        {
            m_pMethod = method;
            m_arguments = arguments;
            m_returnTypeORfieldType = returnType;
            m_managedCallingConvention = callingConvention;
            m_sig = null;
            m_pCallTarget = null;
            m_csig = 0;
            m_numVirtualFixedArgs = 0;
            m_64bitpad = 0;
            m_declaringType = new RuntimeTypeHandle();
        }
        #endregion

        #region Pivate Data Members
        internal RuntimeTypeHandle[] m_arguments;
        internal void* m_sig; 
        internal void* m_pCallTarget;
        internal CallingConventions m_managedCallingConvention;
        internal int m_csig; 
        internal int m_numVirtualFixedArgs; 
        internal int m_64bitpad; 
        internal RuntimeMethodHandle m_pMethod; 
        internal RuntimeTypeHandle m_declaringType; 
        internal RuntimeTypeHandle m_returnTypeORfieldType;        
        #endregion
    }


    internal abstract class Resolver
    {
        internal struct CORINFO_EH_CLAUSE 
        {
            internal int Flags;
            internal int TryOffset;
            internal int TryLength;
            internal int HandlerOffset;
            internal int HandlerLength;
            internal int ClassTokenOrFilterOffset; 
        }

        internal const int COR_ILEXCEPTION_CLAUSE_CACHED_CLASS     = 0x10000000;
        internal const int COR_ILEXCEPTION_CLAUSE_MUST_CACHE_CLASS = 0x20000000;

        internal const int TypeToken    = 0x1;
        internal const int MethodToken  = 0x2;
        internal const int FieldToken   = 0x4;

        // ILHeader info
        internal abstract void GetJitContext(ref int securityControlFlags, ref RuntimeTypeHandle typeOwner);
        internal abstract byte[] GetCodeInfo(ref int stackSize, ref int initLocals, ref int EHCount);
        internal abstract byte[] GetLocalsSignature();
        internal abstract unsafe void GetEHInfo(int EHNumber, void* exception);
        internal abstract unsafe byte[] GetRawEHInfo();
        // token resolution
        internal abstract String GetStringLiteral(int token);
        internal abstract unsafe void* ResolveToken(int token);
        internal abstract unsafe int ParentToken(int token);
        internal abstract byte[] ResolveSignature(int token, int fromMethod);
        internal abstract int IsValidToken(int token);
        internal abstract unsafe void* GetInstantiationInfo(int token);
        // 
        internal abstract MethodInfo GetDynamicMethod();
    }

}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us