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
|
// 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.
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.MethodInfos;
using Internal.Reflection.Core.Execution;
using Internal.Reflection.Tracing;
using Internal.Reflection.Augments;
using EnumInfo = Internal.Runtime.Augments.EnumInfo;
using IRuntimeImplementedType = Internal.Reflection.Core.NonPortable.IRuntimeImplementedType;
using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute;
namespace System.Reflection.Runtime.TypeInfos
{
//
// Abstract base class for all TypeInfo's implemented by the runtime.
//
// This base class performs several services:
//
// - Provides default implementations whenever possible. Some of these
// return the "common" error result for narrowly applicable properties (such as those
// that apply only to generic parameters.)
//
// - Inverts the DeclaredMembers/DeclaredX relationship (DeclaredMembers is auto-implemented, others
// are overriden as abstract. This ordering makes more sense when reading from metadata.)
//
// - Overrides many "NotImplemented" members in TypeInfo with abstracts so failure to implement
// shows up as build error.
//
[DebuggerDisplay("{_debugName}")]
internal abstract partial class RuntimeTypeInfo : TypeInfo, ITraceableTypeMember, ICloneable, IRuntimeImplementedType
{
protected RuntimeTypeInfo()
{
}
public abstract override bool IsTypeDefinition { get; }
public abstract override bool IsGenericTypeDefinition { get; }
protected abstract override bool HasElementTypeImpl();
protected abstract override bool IsArrayImpl();
public abstract override bool IsSZArray { get; }
public abstract override bool IsVariableBoundArray { get; }
protected abstract override bool IsByRefImpl();
protected abstract override bool IsPointerImpl();
public abstract override bool IsGenericParameter { get; }
public abstract override bool IsConstructedGenericType { get; }
public abstract override bool IsByRefLike { get; }
public abstract override Assembly Assembly { get; }
public sealed override string AssemblyQualifiedName
{
get
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_AssemblyQualifiedName(this);
#endif
string fullName = FullName;
if (fullName == null) // Some Types (such as generic parameters) return null for FullName by design.
return null;
string assemblyName = InternalFullNameOfAssembly;
return fullName + ", " + assemblyName;
}
}
public sealed override Type AsType()
{
return this;
}
public sealed override Type BaseType
{
get
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_BaseType(this);
#endif
// If this has a RuntimeTypeHandle, let the underlying runtime engine have the first crack. If it refuses, fall back to metadata.
RuntimeTypeHandle typeHandle = InternalTypeHandleIfAvailable;
if (!typeHandle.IsNull())
{
RuntimeTypeHandle baseTypeHandle;
if (ReflectionCoreExecution.ExecutionEnvironment.TryGetBaseType(typeHandle, out baseTypeHandle))
return Type.GetTypeFromHandle(baseTypeHandle);
}
Type baseType = BaseTypeWithoutTheGenericParameterQuirk;
if (baseType != null && baseType.IsGenericParameter)
{
// Desktop quirk: a generic parameter whose constraint is another generic parameter reports its BaseType as System.Object
// unless that other generic parameter has a "class" constraint.
GenericParameterAttributes genericParameterAttributes = baseType.GenericParameterAttributes;
if (0 == (genericParameterAttributes & GenericParameterAttributes.ReferenceTypeConstraint))
baseType = CommonRuntimeTypes.Object;
}
return baseType;
}
}
public abstract override bool ContainsGenericParameters { get; }
public abstract override IEnumerable<CustomAttributeData> CustomAttributes { get; }
//
// Left unsealed as generic parameter types must override.
//
public override MethodBase DeclaringMethod
{
get
{
Debug.Assert(!IsGenericParameter);
throw new InvalidOperationException(SR.Arg_NotGenericParameter);
}
}
//
// Equals()/GetHashCode()
//
// RuntimeTypeInfo objects are interned to preserve the app-compat rule that Type objects (which are the same as TypeInfo objects)
// can be compared using reference equality.
//
// We use weak pointers to intern the objects. This means we can use instance equality to implement Equals() but we cannot use
// the instance hashcode to implement GetHashCode() (otherwise, the hash code will not be stable if the TypeInfo is released and recreated.)
// Thus, we override and seal Equals() here but defer to a flavor-specific hash code implementation.
//
public sealed override bool Equals(object obj)
{
return object.ReferenceEquals(this, obj);
}
public sealed override bool Equals(Type o)
{
return object.ReferenceEquals(this, o);
}
public sealed override int GetHashCode()
{
return InternalGetHashCode();
}
public abstract override string FullName { get; }
//
// Left unsealed as generic parameter types must override.
//
public override GenericParameterAttributes GenericParameterAttributes
{
get
{
Debug.Assert(!IsGenericParameter);
throw new InvalidOperationException(SR.Arg_NotGenericParameter);
}
}
//
// Left unsealed as generic parameter types must override this.
//
public override int GenericParameterPosition
{
get
{
Debug.Assert(!IsGenericParameter);
throw new InvalidOperationException(SR.Arg_NotGenericParameter);
}
}
public sealed override Type[] GenericTypeArguments
{
get
{
return InternalRuntimeGenericTypeArguments.CloneTypeArray();
}
}
public sealed override MemberInfo[] GetDefaultMembers()
{
string defaultMemberName = GetDefaultMemberName();
return defaultMemberName != null ? GetMember(defaultMemberName) : Array.Empty<MemberInfo>();
}
public sealed override InterfaceMapping GetInterfaceMap(Type interfaceType)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_InterfaceMap);
}
//
// Implements the correct GUID behavior for all "constructed" types (i.e. returning an all-zero GUID.) Left unsealed
// so that RuntimeNamedTypeInfo can override.
//
public override Guid GUID
{
get
{
return Guid.Empty;
}
}
public abstract override bool HasSameMetadataDefinitionAs(MemberInfo other);
public sealed override IEnumerable<Type> ImplementedInterfaces
{
get
{
LowLevelListWithIList<Type> result = new LowLevelListWithIList<Type>();
bool done = false;
// If this has a RuntimeTypeHandle, let the underlying runtime engine have the first crack. If it refuses, fall back to metadata.
RuntimeTypeHandle typeHandle = InternalTypeHandleIfAvailable;
if (!typeHandle.IsNull())
{
IEnumerable<RuntimeTypeHandle> implementedInterfaces = ReflectionCoreExecution.ExecutionEnvironment.TryGetImplementedInterfaces(typeHandle);
if (implementedInterfaces != null)
{
done = true;
foreach (RuntimeTypeHandle th in implementedInterfaces)
{
result.Add(Type.GetTypeFromHandle(th));
}
}
}
if (!done)
{
TypeContext typeContext = this.TypeContext;
Type baseType = this.BaseTypeWithoutTheGenericParameterQuirk;
if (baseType != null)
result.AddRange(baseType.GetInterfaces());
foreach (QTypeDefRefOrSpec directlyImplementedInterface in this.TypeRefDefOrSpecsForDirectlyImplementedInterfaces)
{
Type ifc = directlyImplementedInterface.Resolve(typeContext);
if (result.Contains(ifc))
continue;
result.Add(ifc);
foreach (Type indirectIfc in ifc.GetInterfaces())
{
if (result.Contains(indirectIfc))
continue;
result.Add(indirectIfc);
}
}
}
return result.AsNothingButIEnumerable();
}
}
public sealed override bool IsAssignableFrom(TypeInfo typeInfo) => IsAssignableFrom((Type)typeInfo);
public sealed override bool IsAssignableFrom(Type c)
{
if (c == null)
return false;
if (object.ReferenceEquals(c, this))
return true;
c = c.UnderlyingSystemType;
Type typeInfo = c;
RuntimeTypeInfo toTypeInfo = this;
if (typeInfo == null || !typeInfo.IsRuntimeImplemented())
return false; // Desktop compat: If typeInfo is null, or implemented by a different Reflection implementation, return "false."
RuntimeTypeInfo fromTypeInfo = typeInfo.CastToRuntimeTypeInfo();
if (toTypeInfo.Equals(fromTypeInfo))
return true;
RuntimeTypeHandle toTypeHandle = toTypeInfo.InternalTypeHandleIfAvailable;
RuntimeTypeHandle fromTypeHandle = fromTypeInfo.InternalTypeHandleIfAvailable;
bool haveTypeHandles = !(toTypeHandle.IsNull() || fromTypeHandle.IsNull());
if (haveTypeHandles)
{
// If both types have type handles, let MRT handle this. It's not dependent on metadata.
if (ReflectionCoreExecution.ExecutionEnvironment.IsAssignableFrom(toTypeHandle, fromTypeHandle))
return true;
// Runtime IsAssignableFrom does not handle casts from generic type definitions: always returns false. For those, we fall through to the
// managed implementation. For everyone else, return "false".
//
// Runtime IsAssignableFrom does not handle pointer -> UIntPtr cast.
if (!(fromTypeInfo.IsGenericTypeDefinition || fromTypeInfo.IsPointer))
return false;
}
// If we got here, the types are open, or reduced away, or otherwise lacking in type handles. Perform the IsAssignability check in managed code.
return Assignability.IsAssignableFrom(this, typeInfo);
}
public sealed override bool IsEnum
{
get
{
return 0 != (Classification & TypeClassification.IsEnum);
}
}
public sealed override MemberTypes MemberType
{
get
{
if (IsPublic || IsNotPublic)
return MemberTypes.TypeInfo;
else
return MemberTypes.NestedType;
}
}
//
// Left unsealed as there are so many subclasses. Need to be overriden by EcmaFormatRuntimeNamedTypeInfo and RuntimeConstructedGenericTypeInfo
//
public abstract override int MetadataToken
{
get;
}
public sealed override Module Module
{
get
{
return Assembly.ManifestModule;
}
}
public abstract override string Namespace { get; }
public sealed override Type[] GenericTypeParameters
{
get
{
return RuntimeGenericTypeParameters.CloneTypeArray();
}
}
//
// Left unsealed as array types must override this.
//
public override int GetArrayRank()
{
Debug.Assert(!IsArray);
throw new ArgumentException(SR.Argument_HasToBeArrayClass);
}
public sealed override Type GetElementType()
{
return InternalRuntimeElementType;
}
//
// Left unsealed as generic parameter types must override.
//
public override Type[] GetGenericParameterConstraints()
{
Debug.Assert(!IsGenericParameter);
throw new InvalidOperationException(SR.Arg_NotGenericParameter);
}
//
// Left unsealed as IsGenericType types must override this.
//
public override Type GetGenericTypeDefinition()
{
Debug.Assert(!IsGenericType);
throw new InvalidOperationException(SR.InvalidOperation_NotGenericType);
}
public sealed override Type MakeArrayType()
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_MakeArrayType(this);
#endif
// Do not implement this as a call to MakeArrayType(1) - they are not interchangable. MakeArrayType() returns a
// vector type ("SZArray") while MakeArrayType(1) returns a multidim array of rank 1. These are distinct types
// in the ECMA model and in CLR Reflection.
return this.GetArrayType();
}
public sealed override Type MakeArrayType(int rank)
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_MakeArrayType(this, rank);
#endif
if (rank <= 0)
throw new IndexOutOfRangeException();
return this.GetMultiDimArrayType(rank);
}
public sealed override Type MakeByRefType()
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_MakeByRefType(this);
#endif
return this.GetByRefType();
}
public sealed override Type MakeGenericType(params Type[] typeArguments)
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_MakeGenericType(this, typeArguments);
#endif
if (typeArguments == null)
throw new ArgumentNullException(nameof(typeArguments));
if (!IsGenericTypeDefinition)
throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this));
// We intentionally don't validate the number of arguments or their suitability to the generic type's constraints.
// In a pay-for-play world, this can cause needless MissingMetadataExceptions. There is no harm in creating
// the Type object for an inconsistent generic type - no EEType will ever match it so any attempt to "invoke" it
// will throw an exception.
RuntimeTypeInfo[] runtimeTypeArguments = new RuntimeTypeInfo[typeArguments.Length];
for (int i = 0; i < typeArguments.Length; i++)
{
RuntimeTypeInfo runtimeTypeArgument = typeArguments[i] as RuntimeTypeInfo;
if (runtimeTypeArgument == null)
{
if (typeArguments[i] == null)
throw new ArgumentNullException();
else
throw new PlatformNotSupportedException(SR.PlatformNotSupported_MakeGenericType); // "PlatformNotSupported" because on desktop, passing in a foreign type is allowed and creates a RefEmit.TypeBuilder
}
// Desktop compatibility: Treat generic type definitions as a constructed generic type using the generic parameters as type arguments.
if (runtimeTypeArgument.IsGenericTypeDefinition)
runtimeTypeArgument = runtimeTypeArgument.GetConstructedGenericType(runtimeTypeArgument.RuntimeGenericTypeParameters);
runtimeTypeArguments[i] = runtimeTypeArgument;
}
return this.GetConstructedGenericType(runtimeTypeArguments);
}
public sealed override Type MakePointerType()
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_MakePointerType(this);
#endif
return this.GetPointerType();
}
public sealed override Type DeclaringType
{
get
{
return this.InternalDeclaringType;
}
}
public sealed override string Name
{
get
{
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
ReflectionTrace.TypeInfo_Name(this);
#endif
Type rootCauseForFailure = null;
string name = InternalGetNameIfAvailable(ref rootCauseForFailure);
if (name == null)
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(rootCauseForFailure);
return name;
}
}
public sealed override Type ReflectedType
{
get
{
// Desktop compat: For types, ReflectedType == DeclaringType. Nested types are always looked up as BindingFlags.DeclaredOnly was passed.
// For non-nested types, the concept of a ReflectedType doesn't even make sense.
return DeclaringType;
}
}
public abstract override StructLayoutAttribute StructLayoutAttribute { get; }
public abstract override string ToString();
public sealed override RuntimeTypeHandle TypeHandle
{
get
{
RuntimeTypeHandle typeHandle = InternalTypeHandleIfAvailable;
if (!typeHandle.IsNull())
return typeHandle;
// If a constructed type doesn't have an type handle, it's either because the reducer tossed it (in which case,
// we would thrown a MissingMetadataException when attempting to construct the type) or because one of
// component types contains open type parameters. Since we eliminated the first case, it must be the second.
// Throwing PlatformNotSupported since the desktop does, in fact, create type handles for open types.
if (HasElementType || IsConstructedGenericType || IsGenericParameter)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NoTypeHandleForOpenTypes);
// If got here, this is a "plain old type" that has metadata but no type handle. We can get here if the only
// representation of the type is in the native metadata and there's no EEType at the runtime side.
// If you squint hard, this is a missing metadata situation - the metadata is missing on the runtime side - and
// the action for the user to take is the same: go mess with RD.XML.
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(this);
}
}
public sealed override Type UnderlyingSystemType
{
get
{
return this;
}
}
protected abstract override TypeAttributes GetAttributeFlagsImpl();
protected sealed override TypeCode GetTypeCodeImpl()
{
return ReflectionAugments.GetRuntimeTypeCode(this);
}
protected abstract int InternalGetHashCode();
protected sealed override bool IsCOMObjectImpl()
{
return ReflectionCoreExecution.ExecutionEnvironment.IsCOMObject(this);
}
protected sealed override bool IsPrimitiveImpl()
{
return 0 != (Classification & TypeClassification.IsPrimitive);
}
protected sealed override bool IsValueTypeImpl()
{
return 0 != (Classification & TypeClassification.IsValueType);
}
String ITraceableTypeMember.MemberName
{
get
{
string name = InternalNameIfAvailable;
return name ?? string.Empty;
}
}
Type ITraceableTypeMember.ContainingType
{
get
{
return this.InternalDeclaringType;
}
}
//
// Returns the anchoring typedef that declares the members that this type wants returned by the Declared*** properties.
// The Declared*** properties will project the anchoring typedef's members by overriding their DeclaringType property with "this"
// and substituting the value of this.TypeContext into any generic parameters.
//
// Default implementation returns null which causes the Declared*** properties to return no members.
//
// Note that this does not apply to DeclaredNestedTypes. Nested types and their containers have completely separate generic instantiation environments
// (despite what C# might lead you to think.) Constructed generic types return the exact same same nested types that its generic type definition does
// - i.e. their DeclaringTypes refer back to the generic type definition, not the constructed generic type.)
//
// Note also that we cannot use this anchoring concept for base types because of generic parameters. Generic parameters return
// a base class and interface list based on its constraints.
//
internal virtual RuntimeNamedTypeInfo AnchoringTypeDefinitionForDeclaredMembers
{
get
{
return null;
}
}
internal EnumInfo EnumInfo => Cache.EnumInfo;
internal abstract Type InternalDeclaringType { get; }
//
// Return the full name of the "defining assembly" for the purpose of computing TypeInfo.AssemblyQualifiedName;
//
internal abstract string InternalFullNameOfAssembly { get; }
public abstract override string InternalGetNameIfAvailable(ref Type rootCauseForFailure);
//
// Left unsealed as HasElement types must override this.
//
internal virtual RuntimeTypeInfo InternalRuntimeElementType
{
get
{
Debug.Assert(!HasElementType);
return null;
}
}
//
// Left unsealed as constructed generic types must override this.
//
internal virtual RuntimeTypeInfo[] InternalRuntimeGenericTypeArguments
{
get
{
Debug.Assert(!IsConstructedGenericType);
return Array.Empty<RuntimeTypeInfo>();
}
}
internal abstract RuntimeTypeHandle InternalTypeHandleIfAvailable { get; }
internal bool IsDelegate
{
get
{
return 0 != (Classification & TypeClassification.IsDelegate);
}
}
//
// Returns true if it's possible to ask for a list of members and the base type without triggering a MissingMetadataException.
//
internal abstract bool CanBrowseWithoutMissingMetadataExceptions { get; }
//
// The non-public version of TypeInfo.GenericTypeParameters (does not array-copy.)
//
internal virtual RuntimeTypeInfo[] RuntimeGenericTypeParameters
{
get
{
Debug.Assert(!(this is RuntimeNamedTypeInfo));
return Array.Empty<RuntimeTypeInfo>();
}
}
//
// Normally returns empty: Overridden by array types to return constructors.
//
internal virtual IEnumerable<RuntimeConstructorInfo> SyntheticConstructors
{
get
{
return Empty<RuntimeConstructorInfo>.Enumerable;
}
}
//
// Normally returns empty: Overridden by array types to return the "Get" and "Set" methods.
//
internal virtual IEnumerable<RuntimeMethodInfo> SyntheticMethods
{
get
{
return Empty<RuntimeMethodInfo>.Enumerable;
}
}
//
// Returns the base type as a typeDef, Ref, or Spec. Default behavior is to QTypeDefRefOrSpec.Null, which causes BaseType to return null.
//
// If you override this method, there is no need to override BaseTypeWithoutTheGenericParameterQuirk.
//
internal virtual QTypeDefRefOrSpec TypeRefDefOrSpecForBaseType
{
get
{
return QTypeDefRefOrSpec.Null;
}
}
//
// Returns the *directly implemented* interfaces as typedefs, specs or refs. ImplementedInterfaces will take care of the transitive closure and
// insertion of the TypeContext.
//
internal virtual QTypeDefRefOrSpec[] TypeRefDefOrSpecsForDirectlyImplementedInterfaces
{
get
{
return Array.Empty<QTypeDefRefOrSpec>();
}
}
//
// Returns the generic parameter substitutions to use when enumerating declared members, base class and implemented interfaces.
//
internal virtual TypeContext TypeContext
{
get
{
return new TypeContext(null, null);
}
}
//
// Note: This can be (and is) called multiple times. We do not do this work in the constructor as calling ToString()
// in the constructor causes some serious recursion issues.
//
internal void EstablishDebugName()
{
bool populateDebugNames = DeveloperExperienceState.DeveloperExperienceModeEnabled;
#if DEBUG
populateDebugNames = true;
#endif
if (!populateDebugNames)
return;
if (_debugName == null)
{
_debugName = "Constructing..."; // Protect against any inadvertent reentrancy.
String debugName;
#if ENABLE_REFLECTION_TRACE
if (ReflectionTrace.Enabled)
debugName = this.GetTraceString(); // If tracing on, call this.GetTraceString() which only gives you useful strings when metadata is available but doesn't pollute the ETW trace.
else
#endif
debugName = this.ToString();
if (debugName == null)
debugName = "";
_debugName = debugName;
}
return;
}
//
// This internal method implements BaseType without the following desktop quirk:
//
// class Foo<X,Y>
// where X:Y
// where Y:MyReferenceClass
//
// The desktop reports "X"'s base type as "System.Object" rather than "Y", even though it does
// report any interfaces that are in MyReferenceClass's interface list.
//
// This seriously messes up the implementation of RuntimeTypeInfo.ImplementedInterfaces which assumes
// that it can recover the transitive interface closure by combining the directly mentioned interfaces and
// the BaseType's own interface closure.
//
// To implement this with the least amount of code smell, we'll implement the idealized version of BaseType here
// and make the special-case adjustment in the public version of BaseType.
//
// If you override this method, there is no need to overrride TypeRefDefOrSpecForBaseType.
//
// This method is left unsealed so that RuntimeCLSIDTypeInfo can override.
//
internal virtual Type BaseTypeWithoutTheGenericParameterQuirk
{
get
{
QTypeDefRefOrSpec baseTypeDefRefOrSpec = TypeRefDefOrSpecForBaseType;
RuntimeTypeInfo baseType = null;
if (!baseTypeDefRefOrSpec.IsValid)
{
baseType = baseTypeDefRefOrSpec.Resolve(this.TypeContext);
}
return baseType;
}
}
private string GetDefaultMemberName()
{
Type defaultMemberAttributeType = typeof(DefaultMemberAttribute);
for (Type type = this; type != null; type = type.BaseType)
{
foreach (CustomAttributeData attribute in type.CustomAttributes)
{
if (attribute.AttributeType == defaultMemberAttributeType)
{
// NOTE: Neither indexing nor cast can fail here. Any attempt to use fewer than 1 argument
// or a non-string argument would correctly trigger MissingMethodException before
// we reach here as that would be an attempt to reference a non-existent DefaultMemberAttribute
// constructor.
Debug.Assert(attribute.ConstructorArguments.Count == 1 && attribute.ConstructorArguments[0].Value is string);
string memberName = (string)(attribute.ConstructorArguments[0].Value);
return memberName;
}
}
}
return null;
}
//
// Returns a latched set of flags indicating the value of IsValueType, IsEnum, etc.
//
private TypeClassification Classification
{
get
{
if (_lazyClassification == 0)
{
TypeClassification classification = TypeClassification.Computed;
Type baseType = this.BaseType;
if (baseType != null)
{
Type enumType = CommonRuntimeTypes.Enum;
Type valueType = CommonRuntimeTypes.ValueType;
if (baseType.Equals(enumType))
classification |= TypeClassification.IsEnum | TypeClassification.IsValueType;
if (baseType.Equals(CommonRuntimeTypes.MulticastDelegate))
classification |= TypeClassification.IsDelegate;
if (baseType.Equals(valueType) && !(this.Equals(enumType)))
{
classification |= TypeClassification.IsValueType;
foreach (Type primitiveType in ReflectionCoreExecution.ExecutionDomain.PrimitiveTypes)
{
if (this.Equals(primitiveType))
{
classification |= TypeClassification.IsPrimitive;
break;
}
}
}
}
_lazyClassification = classification;
}
return _lazyClassification;
}
}
[Flags]
private enum TypeClassification
{
Computed = 0x00000001, // Always set (to indicate that the lazy evaluation has occurred)
IsValueType = 0x00000002,
IsEnum = 0x00000004,
IsPrimitive = 0x00000008,
IsDelegate = 0x00000010,
}
object ICloneable.Clone()
{
return this;
}
private volatile TypeClassification _lazyClassification;
private String _debugName;
}
}
|