This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathRtType.cs
5328 lines (4394 loc) · 213 KB
/
RtType.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
//
//
// Implements System.RuntimeType
//
// ======================================================================================
using System;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Globalization;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using MdSigCallingConvention = System.Signature.MdSigCallingConvention;
using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
using System.Runtime.InteropServices;
using DebuggerStepThroughAttribute = System.Diagnostics.DebuggerStepThroughAttribute;
using MdToken = System.Reflection.MetadataToken;
using System.Runtime.Versioning;
namespace System
{
// this is a work around to get the concept of a calli. It's not as fast but it would be interesting to
// see how it compares to the current implementation.
// This delegate will disappear at some point in favor of calli
internal delegate void CtorDelegate(object instance);
// Keep this in sync with FormatFlags defined in typestring.h
internal enum TypeNameFormatFlags
{
FormatBasic = 0x00000000, // Not a bitmask, simply the tersest flag settings possible
FormatNamespace = 0x00000001, // Include namespace and/or enclosing class names in type names
FormatFullInst = 0x00000002, // Include namespace and assembly in generic types (regardless of other flag settings)
FormatAssembly = 0x00000004, // Include assembly display name in type names
FormatSignature = 0x00000008, // Include signature in method names
FormatNoVersion = 0x00000010, // Suppress version and culture information in all assembly names
#if DEBUG
FormatDebug = 0x00000020, // For debug printing of types only
#endif
FormatAngleBrackets = 0x00000040, // Whether generic types are C<T> or C[T]
FormatStubInfo = 0x00000080, // Include stub info like {unbox-stub}
FormatGenericParam = 0x00000100, // Use !name and !!name for generic type and method parameters
}
internal enum TypeNameKind
{
Name,
ToString,
FullName,
}
internal sealed class RuntimeType :
System.Reflection.TypeInfo, ICloneable
{
#region Definitions
internal enum MemberListType
{
All,
CaseSensitive,
CaseInsensitive,
HandleToInfo
}
// Helper to build lists of MemberInfos. Special cased to avoid allocations for lists of one element.
private struct ListBuilder<T> where T : class
{
private T[] _items;
private T _item;
private int _count;
private int _capacity;
public ListBuilder(int capacity)
{
_items = null;
_item = null;
_count = 0;
_capacity = capacity;
}
public T this[int index]
{
get
{
Debug.Assert(index < Count);
return (_items != null) ? _items[index] : _item;
}
}
public T[] ToArray()
{
if (_count == 0)
return Array.Empty<T>();
if (_count == 1)
return new T[1] { _item };
Array.Resize(ref _items, _count);
_capacity = _count;
return _items;
}
public void CopyTo(object[] array, int index)
{
if (_count == 0)
return;
if (_count == 1)
{
array[index] = _item;
return;
}
Array.Copy(_items, 0, array, index, _count);
}
public int Count
{
get
{
return _count;
}
}
public void Add(T item)
{
if (_count == 0)
{
_item = item;
}
else
{
if (_count == 1)
{
if (_capacity < 2)
_capacity = 4;
_items = new T[_capacity];
_items[0] = _item;
}
else
if (_capacity == _count)
{
int newCapacity = 2 * _capacity;
Array.Resize(ref _items, newCapacity);
_capacity = newCapacity;
}
_items[_count] = item;
}
_count++;
}
}
internal class RuntimeTypeCache
{
private const int MAXNAMELEN = 1024;
#region Definitions
internal enum CacheType
{
Method,
Constructor,
Field,
Property,
Event,
Interface,
NestedType
}
private readonly struct Filter
{
private readonly MdUtf8String m_name;
private readonly MemberListType m_listType;
private readonly uint m_nameHash;
public unsafe Filter(byte* pUtf8Name, int cUtf8Name, MemberListType listType)
{
m_name = new MdUtf8String((void*)pUtf8Name, cUtf8Name);
m_listType = listType;
m_nameHash = 0;
if (RequiresStringComparison())
{
m_nameHash = m_name.HashCaseInsensitive();
}
}
public bool Match(MdUtf8String name)
{
bool retVal = true;
if (m_listType == MemberListType.CaseSensitive)
retVal = m_name.Equals(name);
else if (m_listType == MemberListType.CaseInsensitive)
retVal = m_name.EqualsCaseInsensitive(name);
// Currently the callers of UsesStringComparison assume that if it returns false
// then the match always succeeds and can be skipped. Assert that this is maintained.
Debug.Assert(retVal || RequiresStringComparison());
return retVal;
}
// Does the current match type require a string comparison?
// If not, we know Match will always return true and the call can be skipped
// If so, we know we can have a valid hash to check against from GetHashToMatch
public bool RequiresStringComparison()
{
return (m_listType == MemberListType.CaseSensitive) ||
(m_listType == MemberListType.CaseInsensitive);
}
public bool CaseSensitive()
{
return (m_listType == MemberListType.CaseSensitive);
}
public uint GetHashToMatch()
{
Debug.Assert(RequiresStringComparison());
return m_nameHash;
}
}
private class MemberInfoCache<T> where T : MemberInfo
{
#region Private Data Members
// MemberInfo caches
private CerHashtable<string, T[]> m_csMemberInfos;
private CerHashtable<string, T[]> m_cisMemberInfos;
// List of MemberInfos given out. When m_cacheComplete is false, it may have null entries at the end to avoid
// reallocating the list every time a new entry is added.
private T[] m_allMembers;
private bool m_cacheComplete;
// This is the strong reference back to the cache
private RuntimeTypeCache m_runtimeTypeCache;
#endregion
#region Constructor
#if MDA_SUPPORTED
#endif
internal MemberInfoCache(RuntimeTypeCache runtimeTypeCache)
{
#if MDA_SUPPORTED
Mda.MemberInfoCacheCreation();
#endif
m_runtimeTypeCache = runtimeTypeCache;
}
internal MethodBase AddMethod(RuntimeType declaringType, RuntimeMethodHandleInternal method, CacheType cacheType)
{
T[] list = null;
MethodAttributes methodAttributes = RuntimeMethodHandle.GetAttributes(method);
bool isPublic = (methodAttributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
bool isInherited = declaringType != ReflectedType;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
switch (cacheType)
{
case CacheType.Method:
list = (T[])(object)new RuntimeMethodInfo[1] {
new RuntimeMethodInfo(method, declaringType, m_runtimeTypeCache, methodAttributes, bindingFlags, null)
};
break;
case CacheType.Constructor:
list = (T[])(object)new RuntimeConstructorInfo[1] {
new RuntimeConstructorInfo(method, declaringType, m_runtimeTypeCache, methodAttributes, bindingFlags)
};
break;
}
Insert(ref list, null, MemberListType.HandleToInfo);
return (MethodBase)(object)list[0];
}
internal FieldInfo AddField(RuntimeFieldHandleInternal field)
{
// create the runtime field info
FieldAttributes fieldAttributes = RuntimeFieldHandle.GetAttributes(field);
bool isPublic = (fieldAttributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
bool isStatic = (fieldAttributes & FieldAttributes.Static) != 0;
RuntimeType approxDeclaringType = RuntimeFieldHandle.GetApproxDeclaringType(field);
bool isInherited = RuntimeFieldHandle.AcquiresContextFromThis(field) ?
!RuntimeTypeHandle.CompareCanonicalHandles(approxDeclaringType, ReflectedType) :
approxDeclaringType != ReflectedType;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
T[] list = (T[])(object)new RuntimeFieldInfo[1] {
new RtFieldInfo(field, ReflectedType, m_runtimeTypeCache, bindingFlags)
};
Insert(ref list, null, MemberListType.HandleToInfo);
return (FieldInfo)(object)list[0];
}
private unsafe T[] Populate(string name, MemberListType listType, CacheType cacheType)
{
T[] list = null;
if (name == null || name.Length == 0 ||
(cacheType == CacheType.Constructor && name[0] != '.' && name[0] != '*'))
{
list = GetListByName(null, 0, null, 0, listType, cacheType);
}
else
{
int cNameLen = name.Length;
fixed (char* pName = name)
{
int cUtf8Name = Encoding.UTF8.GetByteCount(pName, cNameLen);
// allocating on the stack is faster than allocating on the GC heap
// but we surely don't want to cause a stack overflow
// no one should be looking for a member whose name is longer than 1024
if (cUtf8Name > MAXNAMELEN)
{
byte[] utf8Name = new byte[cUtf8Name];
fixed (byte* pUtf8Name = &utf8Name[0])
{
list = GetListByName(pName, cNameLen, pUtf8Name, cUtf8Name, listType, cacheType);
}
}
else
{
byte* pUtf8Name = stackalloc byte[cUtf8Name];
list = GetListByName(pName, cNameLen, pUtf8Name, cUtf8Name, listType, cacheType);
}
}
}
Insert(ref list, name, listType);
return list;
}
private unsafe T[] GetListByName(char* pName, int cNameLen, byte* pUtf8Name, int cUtf8Name, MemberListType listType, CacheType cacheType)
{
if (cNameLen != 0)
Encoding.UTF8.GetBytes(pName, cNameLen, pUtf8Name, cUtf8Name);
Filter filter = new Filter(pUtf8Name, cUtf8Name, listType);
object list = null;
switch (cacheType)
{
case CacheType.Method:
list = PopulateMethods(filter);
break;
case CacheType.Field:
list = PopulateFields(filter);
break;
case CacheType.Constructor:
list = PopulateConstructors(filter);
break;
case CacheType.Property:
list = PopulateProperties(filter);
break;
case CacheType.Event:
list = PopulateEvents(filter);
break;
case CacheType.NestedType:
list = PopulateNestedClasses(filter);
break;
case CacheType.Interface:
list = PopulateInterfaces(filter);
break;
default:
Debug.Fail("Invalid CacheType");
break;
}
return (T[])list;
}
// May replace the list with a new one if certain cache
// lookups succeed. Also, may modify the contents of the list
// after merging these new data structures with cached ones.
internal void Insert(ref T[] list, string name, MemberListType listType)
{
bool lockTaken = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
Monitor.Enter(this, ref lockTaken);
switch (listType)
{
case MemberListType.CaseSensitive:
{
// Ensure we always return a list that has
// been merged with the global list.
T[] cachedList = m_csMemberInfos[name];
if (cachedList == null)
{
MergeWithGlobalList(list);
m_csMemberInfos[name] = list;
}
else
list = cachedList;
}
break;
case MemberListType.CaseInsensitive:
{
// Ensure we always return a list that has
// been merged with the global list.
T[] cachedList = m_cisMemberInfos[name];
if (cachedList == null)
{
MergeWithGlobalList(list);
m_cisMemberInfos[name] = list;
}
else
list = cachedList;
}
break;
case MemberListType.All:
if (!m_cacheComplete)
{
MergeWithGlobalList(list);
// Trim null entries at the end of m_allMembers array
int memberCount = m_allMembers.Length;
while (memberCount > 0)
{
if (m_allMembers[memberCount - 1] != null)
break;
memberCount--;
}
Array.Resize(ref m_allMembers, memberCount);
Volatile.Write(ref m_cacheComplete, true);
}
list = m_allMembers;
break;
default:
MergeWithGlobalList(list);
break;
}
}
finally
{
if (lockTaken)
{
Monitor.Exit(this);
}
}
}
// Modifies the existing list.
private void MergeWithGlobalList(T[] list)
{
T[] cachedMembers = m_allMembers;
if (cachedMembers == null)
{
m_allMembers = list;
return;
}
int cachedCount = cachedMembers.Length;
int freeSlotIndex = 0;
for (int i = 0; i < list.Length; i++)
{
T newMemberInfo = list[i];
bool foundInCache = false;
int cachedIndex;
for (cachedIndex = 0; cachedIndex < cachedCount; cachedIndex++)
{
T cachedMemberInfo = cachedMembers[cachedIndex];
if (cachedMemberInfo == null)
break;
if (newMemberInfo.CacheEquals(cachedMemberInfo))
{
list[i] = cachedMemberInfo;
foundInCache = true;
break;
}
}
if (!foundInCache)
{
if (freeSlotIndex == 0)
freeSlotIndex = cachedIndex;
if (freeSlotIndex >= cachedMembers.Length)
{
int newSize;
if (m_cacheComplete)
{
//
// In theory, we should never add more elements to the cache when it is complete.
//
// Unfortunately, we shipped with bugs that cause changes of the complete cache (DevDiv #339308).
// Grow the list by exactly one element in this case to avoid null entries at the end.
//
Debug.Assert(false);
newSize = cachedMembers.Length + 1;
}
else
{
newSize = Math.Max(Math.Max(4, 2 * cachedMembers.Length), list.Length);
}
// Use different variable for ref argument to Array.Resize to allow enregistration of cachedMembers by the JIT
T[] cachedMembers2 = cachedMembers;
Array.Resize(ref cachedMembers2, newSize);
cachedMembers = cachedMembers2;
}
Debug.Assert(cachedMembers[freeSlotIndex] == null);
cachedMembers[freeSlotIndex] = newMemberInfo;
freeSlotIndex++;
}
}
m_allMembers = cachedMembers;
}
#endregion
#region Population Logic
private unsafe RuntimeMethodInfo[] PopulateMethods(Filter filter)
{
ListBuilder<RuntimeMethodInfo> list = new ListBuilder<RuntimeMethodInfo>();
RuntimeType declaringType = ReflectedType;
Debug.Assert(declaringType != null);
if (RuntimeTypeHandle.IsInterface(declaringType))
{
#region IsInterface
foreach (RuntimeMethodHandleInternal methodHandle in RuntimeTypeHandle.GetIntroducedMethods(declaringType))
{
if (filter.RequiresStringComparison())
{
if (!RuntimeMethodHandle.MatchesNameHash(methodHandle, filter.GetHashToMatch()))
{
Debug.Assert(!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)));
continue;
}
if (!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)))
continue;
}
#region Loop through all methods on the interface
Debug.Assert(!methodHandle.IsNullHandle());
MethodAttributes methodAttributes = RuntimeMethodHandle.GetAttributes(methodHandle);
#region Continue if this is a constructor
Debug.Assert(
(RuntimeMethodHandle.GetAttributes(methodHandle) & MethodAttributes.RTSpecialName) == 0 ||
RuntimeMethodHandle.GetName(methodHandle).Equals(".cctor"));
if ((methodAttributes & MethodAttributes.RTSpecialName) != 0)
continue;
#endregion
#region Calculate Binding Flags
bool isPublic = (methodAttributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
bool isInherited = false;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
#endregion
// get the unboxing stub or instantiating stub if needed
RuntimeMethodHandleInternal instantiatedHandle = RuntimeMethodHandle.GetStubIfNeeded(methodHandle, declaringType, null);
RuntimeMethodInfo runtimeMethodInfo = new RuntimeMethodInfo(
instantiatedHandle, declaringType, m_runtimeTypeCache, methodAttributes, bindingFlags, null);
list.Add(runtimeMethodInfo);
#endregion
}
#endregion
}
else
{
#region IsClass or GenericParameter
while (RuntimeTypeHandle.IsGenericVariable(declaringType))
declaringType = declaringType.GetBaseType();
int numVirtuals = RuntimeTypeHandle.GetNumVirtuals(declaringType);
bool* overrides = stackalloc bool[numVirtuals];
new Span<bool>(overrides, numVirtuals).Clear();
bool isValueType = declaringType.IsValueType;
do
{
int vtableSlots = RuntimeTypeHandle.GetNumVirtuals(declaringType);
foreach (RuntimeMethodHandleInternal methodHandle in RuntimeTypeHandle.GetIntroducedMethods(declaringType))
{
if (filter.RequiresStringComparison())
{
if (!RuntimeMethodHandle.MatchesNameHash(methodHandle, filter.GetHashToMatch()))
{
Debug.Assert(!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)));
continue;
}
if (!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)))
continue;
}
#region Loop through all methods on the current type
Debug.Assert(!methodHandle.IsNullHandle());
MethodAttributes methodAttributes = RuntimeMethodHandle.GetAttributes(methodHandle);
MethodAttributes methodAccess = methodAttributes & MethodAttributes.MemberAccessMask;
#region Continue if this is a constructor
Debug.Assert(
(RuntimeMethodHandle.GetAttributes(methodHandle) & MethodAttributes.RTSpecialName) == 0 ||
RuntimeMethodHandle.GetName(methodHandle).Equals(".ctor") ||
RuntimeMethodHandle.GetName(methodHandle).Equals(".cctor"));
if ((methodAttributes & MethodAttributes.RTSpecialName) != 0)
continue;
#endregion
#region Continue if this is a private declared on a base type
bool isVirtual = false;
int methodSlot = 0;
if ((methodAttributes & MethodAttributes.Virtual) != 0)
{
// only virtual if actually in the vtableslot range, but GetSlot will
// assert if an EnC method, which can't be virtual, so narrow down first
// before calling GetSlot
methodSlot = RuntimeMethodHandle.GetSlot(methodHandle);
isVirtual = (methodSlot < vtableSlots);
}
bool isInherited = declaringType != ReflectedType;
bool isPrivate = methodAccess == MethodAttributes.Private;
if (isInherited && isPrivate && !isVirtual)
continue;
#endregion
#region Continue if this is a virtual and is already overridden
if (isVirtual)
{
Debug.Assert(
(methodAttributes & MethodAttributes.Abstract) != 0 ||
(methodAttributes & MethodAttributes.Virtual) != 0 ||
RuntimeMethodHandle.GetDeclaringType(methodHandle) != declaringType);
if (overrides[methodSlot] == true)
continue;
overrides[methodSlot] = true;
}
else if (isValueType)
{
if ((methodAttributes & (MethodAttributes.Virtual | MethodAttributes.Abstract)) != 0)
continue;
}
else
{
Debug.Assert((methodAttributes & (MethodAttributes.Virtual | MethodAttributes.Abstract)) == 0);
}
#endregion
#region Calculate Binding Flags
bool isPublic = methodAccess == MethodAttributes.Public;
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
#endregion
// get the unboxing stub or instantiating stub if needed
RuntimeMethodHandleInternal instantiatedHandle = RuntimeMethodHandle.GetStubIfNeeded(methodHandle, declaringType, null);
RuntimeMethodInfo runtimeMethodInfo = new RuntimeMethodInfo(
instantiatedHandle, declaringType, m_runtimeTypeCache, methodAttributes, bindingFlags, null);
list.Add(runtimeMethodInfo);
#endregion
}
declaringType = RuntimeTypeHandle.GetBaseType(declaringType);
} while (declaringType != null);
#endregion
}
return list.ToArray();
}
private RuntimeConstructorInfo[] PopulateConstructors(Filter filter)
{
if (ReflectedType.IsGenericParameter)
{
return Array.Empty<RuntimeConstructorInfo>();
}
ListBuilder<RuntimeConstructorInfo> list = new ListBuilder<RuntimeConstructorInfo>();
RuntimeType declaringType = ReflectedType;
foreach (RuntimeMethodHandleInternal methodHandle in RuntimeTypeHandle.GetIntroducedMethods(declaringType))
{
if (filter.RequiresStringComparison())
{
if (!RuntimeMethodHandle.MatchesNameHash(methodHandle, filter.GetHashToMatch()))
{
Debug.Assert(!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)));
continue;
}
if (!filter.Match(RuntimeMethodHandle.GetUtf8Name(methodHandle)))
continue;
}
MethodAttributes methodAttributes = RuntimeMethodHandle.GetAttributes(methodHandle);
Debug.Assert(!methodHandle.IsNullHandle());
if ((methodAttributes & MethodAttributes.RTSpecialName) == 0)
continue;
// Constructors should not be virtual or abstract
Debug.Assert(
(methodAttributes & MethodAttributes.Abstract) == 0 &&
(methodAttributes & MethodAttributes.Virtual) == 0);
#region Calculate Binding Flags
bool isPublic = (methodAttributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
bool isInherited = false;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
#endregion
// get the unboxing stub or instantiating stub if needed
RuntimeMethodHandleInternal instantiatedHandle = RuntimeMethodHandle.GetStubIfNeeded(methodHandle, declaringType, null);
RuntimeConstructorInfo runtimeConstructorInfo =
new RuntimeConstructorInfo(instantiatedHandle, ReflectedType, m_runtimeTypeCache, methodAttributes, bindingFlags);
list.Add(runtimeConstructorInfo);
}
return list.ToArray();
}
private unsafe RuntimeFieldInfo[] PopulateFields(Filter filter)
{
ListBuilder<RuntimeFieldInfo> list = new ListBuilder<RuntimeFieldInfo>();
RuntimeType declaringType = ReflectedType;
#region Populate all static, instance and literal fields
while (RuntimeTypeHandle.IsGenericVariable(declaringType))
declaringType = declaringType.GetBaseType();
while (declaringType != null)
{
PopulateRtFields(filter, declaringType, ref list);
PopulateLiteralFields(filter, declaringType, ref list);
declaringType = RuntimeTypeHandle.GetBaseType(declaringType);
}
#endregion
#region Populate Literal Fields on Interfaces
if (ReflectedType.IsGenericParameter)
{
Type[] interfaces = ReflectedType.BaseType.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++)
{
// Populate literal fields defined on any of the interfaces implemented by the declaring type
PopulateLiteralFields(filter, (RuntimeType)interfaces[i], ref list);
PopulateRtFields(filter, (RuntimeType)interfaces[i], ref list);
}
}
else
{
Type[] interfaces = RuntimeTypeHandle.GetInterfaces(ReflectedType);
if (interfaces != null)
{
for (int i = 0; i < interfaces.Length; i++)
{
// Populate literal fields defined on any of the interfaces implemented by the declaring type
PopulateLiteralFields(filter, (RuntimeType)interfaces[i], ref list);
PopulateRtFields(filter, (RuntimeType)interfaces[i], ref list);
}
}
}
#endregion
return list.ToArray();
}
private unsafe void PopulateRtFields(Filter filter, RuntimeType declaringType, ref ListBuilder<RuntimeFieldInfo> list)
{
IntPtr* pResult = stackalloc IntPtr[64];
int count = 64;
if (!RuntimeTypeHandle.GetFields(declaringType, pResult, &count))
{
fixed (IntPtr* pBigResult = new IntPtr[count])
{
RuntimeTypeHandle.GetFields(declaringType, pBigResult, &count);
PopulateRtFields(filter, pBigResult, count, declaringType, ref list);
}
}
else if (count > 0)
{
PopulateRtFields(filter, pResult, count, declaringType, ref list);
}
}
private unsafe void PopulateRtFields(Filter filter,
IntPtr* ppFieldHandles, int count, RuntimeType declaringType, ref ListBuilder<RuntimeFieldInfo> list)
{
Debug.Assert(declaringType != null);
Debug.Assert(ReflectedType != null);
bool needsStaticFieldForGeneric = RuntimeTypeHandle.HasInstantiation(declaringType) && !RuntimeTypeHandle.ContainsGenericVariables(declaringType);
bool isInherited = declaringType != ReflectedType;
for (int i = 0; i < count; i++)
{
RuntimeFieldHandleInternal runtimeFieldHandle = new RuntimeFieldHandleInternal(ppFieldHandles[i]);
if (filter.RequiresStringComparison())
{
if (!RuntimeFieldHandle.MatchesNameHash(runtimeFieldHandle, filter.GetHashToMatch()))
{
Debug.Assert(!filter.Match(RuntimeFieldHandle.GetUtf8Name(runtimeFieldHandle)));
continue;
}
if (!filter.Match(RuntimeFieldHandle.GetUtf8Name(runtimeFieldHandle)))
continue;
}
Debug.Assert(!runtimeFieldHandle.IsNullHandle());
FieldAttributes fieldAttributes = RuntimeFieldHandle.GetAttributes(runtimeFieldHandle);
FieldAttributes fieldAccess = fieldAttributes & FieldAttributes.FieldAccessMask;
if (isInherited)
{
if (fieldAccess == FieldAttributes.Private)
continue;
}
#region Calculate Binding Flags
bool isPublic = fieldAccess == FieldAttributes.Public;
bool isStatic = (fieldAttributes & FieldAttributes.Static) != 0;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
#endregion
// correct the FieldDesc if needed
if (needsStaticFieldForGeneric && isStatic)
runtimeFieldHandle = RuntimeFieldHandle.GetStaticFieldForGenericType(runtimeFieldHandle, declaringType);
RuntimeFieldInfo runtimeFieldInfo =
new RtFieldInfo(runtimeFieldHandle, declaringType, m_runtimeTypeCache, bindingFlags);
list.Add(runtimeFieldInfo);
}
}
private unsafe void PopulateLiteralFields(Filter filter, RuntimeType declaringType, ref ListBuilder<RuntimeFieldInfo> list)
{
Debug.Assert(declaringType != null);
Debug.Assert(ReflectedType != null);
int tkDeclaringType = RuntimeTypeHandle.GetToken(declaringType);
// Our policy is that TypeDescs do not have metadata tokens
if (MdToken.IsNullToken(tkDeclaringType))
return;
MetadataImport scope = RuntimeTypeHandle.GetMetadataImport(declaringType);
MetadataEnumResult tkFields;
scope.EnumFields(tkDeclaringType, out tkFields);
for (int i = 0; i < tkFields.Length; i++)
{
int tkField = tkFields[i];
Debug.Assert(MdToken.IsTokenOfType(tkField, MetadataTokenType.FieldDef));
Debug.Assert(!MdToken.IsNullToken(tkField));
FieldAttributes fieldAttributes;
scope.GetFieldDefProps(tkField, out fieldAttributes);
FieldAttributes fieldAccess = fieldAttributes & FieldAttributes.FieldAccessMask;
if ((fieldAttributes & FieldAttributes.Literal) != 0)
{
bool isInherited = declaringType != ReflectedType;
if (isInherited)
{
bool isPrivate = fieldAccess == FieldAttributes.Private;
if (isPrivate)
continue;
}
if (filter.RequiresStringComparison())
{
MdUtf8String name;
name = scope.GetName(tkField);
if (!filter.Match(name))
continue;
}
#region Calculate Binding Flags
bool isPublic = fieldAccess == FieldAttributes.Public;
bool isStatic = (fieldAttributes & FieldAttributes.Static) != 0;
BindingFlags bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
#endregion
RuntimeFieldInfo runtimeFieldInfo =
new MdFieldInfo(tkField, fieldAttributes, declaringType.GetTypeHandleInternal(), m_runtimeTypeCache, bindingFlags);
list.Add(runtimeFieldInfo);
}
}
}
private void AddSpecialInterface(ref ListBuilder<RuntimeType> list, Filter filter, RuntimeType iList, bool addSubInterface)
{
if (iList.IsAssignableFrom(ReflectedType))
{
if (filter.Match(RuntimeTypeHandle.GetUtf8Name(iList)))
list.Add(iList);
if (addSubInterface)
{
Type[] iFaces = iList.GetInterfaces();
for (int j = 0; j < iFaces.Length; j++)
{
RuntimeType iFace = (RuntimeType)iFaces[j];
if (iFace.IsGenericType && filter.Match(RuntimeTypeHandle.GetUtf8Name(iFace)))
list.Add(iFace);
}
}
}
}
private RuntimeType[] PopulateInterfaces(Filter filter)
{
ListBuilder<RuntimeType> list = new ListBuilder<RuntimeType>();
RuntimeType declaringType = ReflectedType;
if (!RuntimeTypeHandle.IsGenericVariable(declaringType))
{
Type[] ifaces = RuntimeTypeHandle.GetInterfaces(declaringType);
if (ifaces != null)
{
for (int i = 0; i < ifaces.Length; i++)
{
RuntimeType interfaceType = (RuntimeType)ifaces[i];
if (filter.RequiresStringComparison())
{
if (!filter.Match(RuntimeTypeHandle.GetUtf8Name(interfaceType)))
continue;
}