Add IsCollectible property to Memberinfo and MethodInfo (#21155)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / shared / System / Type.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Threading;
6 using System.Reflection;
7 using System.Diagnostics;
8 using System.Globalization;
9 using System.Runtime.InteropServices;
10
11 namespace System
12 {
13     public abstract partial class Type : MemberInfo, IReflect
14     {
15         protected Type() { }
16
17         public override MemberTypes MemberType => MemberTypes.TypeInfo;
18
19         public new Type GetType() => base.GetType();
20
21         public abstract string Namespace { get; }
22         public abstract string AssemblyQualifiedName { get; }
23         public abstract string FullName { get; }
24
25         public abstract Assembly Assembly { get; }
26         public abstract new Module Module { get; }
27
28         public bool IsNested => DeclaringType != null;
29         public override Type DeclaringType => null;
30         public virtual MethodBase DeclaringMethod => null;
31
32         public override Type ReflectedType => null;
33         public abstract Type UnderlyingSystemType { get; }
34
35         public virtual bool IsTypeDefinition { get { throw NotImplemented.ByDesign; } }
36         public bool IsArray => IsArrayImpl();
37         protected abstract bool IsArrayImpl();
38         public bool IsByRef => IsByRefImpl();
39         protected abstract bool IsByRefImpl();
40         public bool IsPointer => IsPointerImpl();
41         protected abstract bool IsPointerImpl();
42         public virtual bool IsConstructedGenericType { get { throw NotImplemented.ByDesign; } }
43         public virtual bool IsGenericParameter => false;
44         public virtual bool IsGenericTypeParameter => IsGenericParameter && DeclaringMethod == null;
45         public virtual bool IsGenericMethodParameter => IsGenericParameter && DeclaringMethod != null;
46         public virtual bool IsGenericType => false;
47         public virtual bool IsGenericTypeDefinition => false;
48
49         public virtual bool IsSZArray { get { throw NotImplemented.ByDesign; } }
50         public virtual bool IsVariableBoundArray => IsArray && !IsSZArray;
51
52         public virtual bool IsByRefLike => throw new NotSupportedException(SR.NotSupported_SubclassOverride);
53
54         public bool HasElementType => HasElementTypeImpl();
55         protected abstract bool HasElementTypeImpl();
56         public abstract Type GetElementType();
57
58         public virtual int GetArrayRank() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
59
60         public virtual Type GetGenericTypeDefinition() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
61         public virtual Type[] GenericTypeArguments => (IsGenericType && !IsGenericTypeDefinition) ? GetGenericArguments() : Array.Empty<Type>();
62         public virtual Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
63
64         public virtual int GenericParameterPosition { get { throw new InvalidOperationException(SR.Arg_NotGenericParameter); } }
65         public virtual GenericParameterAttributes GenericParameterAttributes { get { throw new NotSupportedException(); } }
66         public virtual Type[] GetGenericParameterConstraints()
67         {
68             if (!IsGenericParameter)
69                 throw new InvalidOperationException(SR.Arg_NotGenericParameter);
70             throw new InvalidOperationException();
71         }
72
73         public TypeAttributes Attributes => GetAttributeFlagsImpl();
74         protected abstract TypeAttributes GetAttributeFlagsImpl();
75
76         public bool IsAbstract => (GetAttributeFlagsImpl() & TypeAttributes.Abstract) != 0;
77         public bool IsImport => (GetAttributeFlagsImpl() & TypeAttributes.Import) != 0;
78         public bool IsSealed => (GetAttributeFlagsImpl() & TypeAttributes.Sealed) != 0;
79         public bool IsSpecialName => (GetAttributeFlagsImpl() & TypeAttributes.SpecialName) != 0;
80
81         public bool IsClass => (GetAttributeFlagsImpl() & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class && !IsValueType;
82
83         public bool IsNestedAssembly => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
84         public bool IsNestedFamANDAssem => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
85         public bool IsNestedFamily => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
86         public bool IsNestedFamORAssem => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
87         public bool IsNestedPrivate => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
88         public bool IsNestedPublic => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
89         public bool IsNotPublic => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
90         public bool IsPublic => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
91
92         public bool IsAutoLayout => (GetAttributeFlagsImpl() & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
93         public bool IsExplicitLayout => (GetAttributeFlagsImpl() & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
94         public bool IsLayoutSequential => (GetAttributeFlagsImpl() & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
95
96         public bool IsAnsiClass => (GetAttributeFlagsImpl() & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass;
97         public bool IsAutoClass => (GetAttributeFlagsImpl() & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
98         public bool IsUnicodeClass => (GetAttributeFlagsImpl() & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
99
100         public bool IsCOMObject => IsCOMObjectImpl();
101         protected abstract bool IsCOMObjectImpl();
102         public bool IsContextful => IsContextfulImpl();
103         protected virtual bool IsContextfulImpl() => false;
104
105         public virtual bool IsEnum => IsSubclassOf(typeof(Enum));
106         public bool IsMarshalByRef => IsMarshalByRefImpl();
107         protected virtual bool IsMarshalByRefImpl() => false;
108         public bool IsPrimitive => IsPrimitiveImpl();
109         protected abstract bool IsPrimitiveImpl();
110         public bool IsValueType => IsValueTypeImpl();
111         protected virtual bool IsValueTypeImpl() => IsSubclassOf(typeof(ValueType));
112
113         public virtual bool IsSignatureType => false;
114
115         public virtual bool IsSecurityCritical { get { throw NotImplemented.ByDesign; } }
116         public virtual bool IsSecuritySafeCritical { get { throw NotImplemented.ByDesign; } }
117         public virtual bool IsSecurityTransparent { get { throw NotImplemented.ByDesign; } }
118
119         public virtual StructLayoutAttribute StructLayoutAttribute { get { throw new NotSupportedException(); } }
120         public ConstructorInfo TypeInitializer => GetConstructorImpl(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, Type.EmptyTypes, null);
121
122         public ConstructorInfo GetConstructor(Type[] types) => GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, types, null);
123         public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) => GetConstructor(bindingAttr, binder, CallingConventions.Any, types, modifiers);
124         public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
125         {
126             if (types == null)
127                 throw new ArgumentNullException(nameof(types));
128             for (int i = 0; i < types.Length; i++)
129             {
130                 if (types[i] == null)
131                     throw new ArgumentNullException(nameof(types));
132             }
133             return GetConstructorImpl(bindingAttr, binder, callConvention, types, modifiers);
134         }
135         protected abstract ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers);
136
137         public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance);
138         public abstract ConstructorInfo[] GetConstructors(BindingFlags bindingAttr);
139
140         public EventInfo GetEvent(string name) => GetEvent(name, Type.DefaultLookup);
141         public abstract EventInfo GetEvent(string name, BindingFlags bindingAttr);
142
143         public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup);
144         public abstract EventInfo[] GetEvents(BindingFlags bindingAttr);
145
146         public FieldInfo GetField(string name) => GetField(name, Type.DefaultLookup);
147         public abstract FieldInfo GetField(string name, BindingFlags bindingAttr);
148
149         public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup);
150         public abstract FieldInfo[] GetFields(BindingFlags bindingAttr);
151
152         public MemberInfo[] GetMember(string name) => GetMember(name, Type.DefaultLookup);
153         public virtual MemberInfo[] GetMember(string name, BindingFlags bindingAttr) => GetMember(name, MemberTypes.All, bindingAttr);
154         public virtual MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
155
156         public MemberInfo[] GetMembers() => GetMembers(Type.DefaultLookup);
157         public abstract MemberInfo[] GetMembers(BindingFlags bindingAttr);
158
159         public MethodInfo GetMethod(string name) => GetMethod(name, Type.DefaultLookup);
160         public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
161         {
162             if (name == null)
163                 throw new ArgumentNullException(nameof(name));
164             return GetMethodImpl(name, bindingAttr, null, CallingConventions.Any, null, null);
165         }
166
167         public MethodInfo GetMethod(string name, Type[] types) => GetMethod(name, types, null);
168         public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers) => GetMethod(name, Type.DefaultLookup, null, types, modifiers);
169         public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) => GetMethod(name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
170         public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
171         {
172             if (name == null)
173                 throw new ArgumentNullException(nameof(name));
174             if (types == null)
175                 throw new ArgumentNullException(nameof(types));
176             for (int i = 0; i < types.Length; i++)
177             {
178                 if (types[i] == null)
179                     throw new ArgumentNullException(nameof(types));
180             }
181             return GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
182         }
183
184         protected abstract MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers);
185
186         public MethodInfo GetMethod(string name, int genericParameterCount, Type[] types) => GetMethod(name, genericParameterCount, types, null);
187         public MethodInfo GetMethod(string name, int genericParameterCount, Type[] types, ParameterModifier[] modifiers) => GetMethod(name, genericParameterCount, Type.DefaultLookup, null, types, modifiers);
188         public MethodInfo GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) => GetMethod(name, genericParameterCount, bindingAttr, binder, CallingConventions.Any, types, modifiers);
189         public MethodInfo GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
190         {
191             if (name == null)
192                 throw new ArgumentNullException(nameof(name));
193             if (genericParameterCount < 0)
194                 throw new ArgumentException(SR.ArgumentOutOfRange_NeedNonNegNum, nameof(genericParameterCount));
195             if (types == null)
196                 throw new ArgumentNullException(nameof(types));
197             for (int i = 0; i < types.Length; i++)
198             {
199                 if (types[i] == null)
200                     throw new ArgumentNullException(nameof(types));
201             }
202             return GetMethodImpl(name, genericParameterCount, bindingAttr, binder, callConvention, types, modifiers);
203         }
204
205         protected virtual MethodInfo GetMethodImpl(string name, int genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) => throw new NotSupportedException();
206
207         public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup);
208         public abstract MethodInfo[] GetMethods(BindingFlags bindingAttr);
209
210         public Type GetNestedType(string name) => GetNestedType(name, Type.DefaultLookup);
211         public abstract Type GetNestedType(string name, BindingFlags bindingAttr);
212
213         public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup);
214         public abstract Type[] GetNestedTypes(BindingFlags bindingAttr);
215
216         public PropertyInfo GetProperty(string name) => GetProperty(name, Type.DefaultLookup);
217         public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
218         {
219             if (name == null)
220                 throw new ArgumentNullException(nameof(name));
221             return GetPropertyImpl(name, bindingAttr, null, null, null, null);
222         }
223
224         public PropertyInfo GetProperty(string name, Type returnType)
225         {
226             if (name == null)
227                 throw new ArgumentNullException(nameof(name));
228             if (returnType == null)
229                 throw new ArgumentNullException(nameof(returnType));
230             return GetPropertyImpl(name, Type.DefaultLookup, null, returnType, null, null);
231         }
232
233         public PropertyInfo GetProperty(string name, Type[] types) => GetProperty(name, null, types);
234         public PropertyInfo GetProperty(string name, Type returnType, Type[] types) => GetProperty(name, returnType, types, null);
235         public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers) => GetProperty(name, Type.DefaultLookup, null, returnType, types, modifiers);
236         public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
237         {
238             if (name == null)
239                 throw new ArgumentNullException(nameof(name));
240             if (types == null)
241                 throw new ArgumentNullException(nameof(types));
242             return GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);
243         }
244
245         protected abstract PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);
246
247         public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup);
248         public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);
249
250         public virtual MemberInfo[] GetDefaultMembers() { throw NotImplemented.ByDesign; }
251
252         public virtual RuntimeTypeHandle TypeHandle { get { throw new NotSupportedException(); } }
253         public static RuntimeTypeHandle GetTypeHandle(object o)
254         {
255             if (o == null)
256                 throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
257             Type type = o.GetType();
258             return type.TypeHandle;
259         }
260
261         public static Type[] GetTypeArray(object[] args)
262         {
263             if (args == null)
264                 throw new ArgumentNullException(nameof(args));
265
266             Type[] cls = new Type[args.Length];
267             for (int i = 0; i < cls.Length; i++)
268             {
269                 if (args[i] == null)
270                     throw new ArgumentNullException();
271                 cls[i] = args[i].GetType();
272             }
273             return cls;
274         }
275
276         public static TypeCode GetTypeCode(Type type)
277         {
278             if (type == null)
279                 return TypeCode.Empty;
280             return type.GetTypeCodeImpl();
281         }
282         protected virtual TypeCode GetTypeCodeImpl()
283         {
284             if (this != UnderlyingSystemType && UnderlyingSystemType != null)
285                 return Type.GetTypeCode(UnderlyingSystemType);
286
287             return TypeCode.Object;
288         }
289
290         public abstract Guid GUID { get; }
291
292         public static Type GetTypeFromCLSID(Guid clsid) => GetTypeFromCLSID(clsid, null, throwOnError: false);
293         public static Type GetTypeFromCLSID(Guid clsid, bool throwOnError) => GetTypeFromCLSID(clsid, null, throwOnError: throwOnError);
294         public static Type GetTypeFromCLSID(Guid clsid, string server) => GetTypeFromCLSID(clsid, server, throwOnError: false);
295
296         public static Type GetTypeFromProgID(string progID) => GetTypeFromProgID(progID, null, throwOnError: false);
297         public static Type GetTypeFromProgID(string progID, bool throwOnError) => GetTypeFromProgID(progID, null, throwOnError: throwOnError);
298         public static Type GetTypeFromProgID(string progID, string server) => GetTypeFromProgID(progID, server, throwOnError: false);
299
300         public abstract Type BaseType { get; }
301
302         [DebuggerHidden]
303         [DebuggerStepThrough]
304         public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args) => InvokeMember(name, invokeAttr, binder, target, args, null, null, null);
305
306         [DebuggerHidden]
307         [DebuggerStepThrough]
308         public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, CultureInfo culture) => InvokeMember(name, invokeAttr, binder, target, args, null, culture, null);
309         public abstract object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters);
310
311         public Type GetInterface(string name) => GetInterface(name, ignoreCase: false);
312         public abstract Type GetInterface(string name, bool ignoreCase);
313         public abstract Type[] GetInterfaces();
314
315         public virtual InterfaceMapping GetInterfaceMap(Type interfaceType) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
316
317         public virtual bool IsInstanceOfType(object o) => o == null ? false : IsAssignableFrom(o.GetType());
318         public virtual bool IsEquivalentTo(Type other) => this == other;
319
320         public virtual Type GetEnumUnderlyingType()
321         {
322             if (!IsEnum)
323                 throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
324
325             FieldInfo[] fields = GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
326             if (fields == null || fields.Length != 1)
327                 throw new ArgumentException(SR.Argument_InvalidEnum, "enumType");
328
329             return fields[0].FieldType;
330         }
331         public virtual Array GetEnumValues()
332         {
333             if (!IsEnum)
334                 throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
335
336             // We don't support GetEnumValues in the default implementation because we cannot create an array of
337             // a non-runtime type. If there is strong need we can consider returning an object or int64 array.
338             throw NotImplemented.ByDesign;
339         }
340
341         public virtual Type MakeArrayType() { throw new NotSupportedException(); }
342         public virtual Type MakeArrayType(int rank) { throw new NotSupportedException(); }
343         public virtual Type MakeByRefType() { throw new NotSupportedException(); }
344         public virtual Type MakeGenericType(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
345         public virtual Type MakePointerType() { throw new NotSupportedException(); }
346
347         public static Type MakeGenericSignatureType(Type genericTypeDefinition, params Type[] typeArguments) => new SignatureConstructedGenericType(genericTypeDefinition, typeArguments);
348
349         public static Type MakeGenericMethodParameter(int position)
350         {
351             if (position < 0)
352                 throw new ArgumentException(SR.ArgumentOutOfRange_NeedNonNegNum, nameof(position));
353             return new SignatureGenericMethodParameterType(position);
354         }
355
356         public override string ToString() => "Type: " + Name;  // Why do we add the "Type: " prefix?
357
358         public override bool Equals(object o) => o == null ? false : Equals(o as Type);
359         public override int GetHashCode()
360         {
361             Type systemType = UnderlyingSystemType;
362             if (!object.ReferenceEquals(systemType, this))
363                 return systemType.GetHashCode();
364             return base.GetHashCode();
365         }
366         public virtual bool Equals(Type o) => o == null ? false : object.ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType);
367
368         public static Type ReflectionOnlyGetType(string typeName, bool throwIfNotFound, bool ignoreCase) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
369
370         public static Binder DefaultBinder
371         {
372             get
373             {
374                 if (s_defaultBinder == null)
375                 {
376                     DefaultBinder binder = new DefaultBinder();
377                     Interlocked.CompareExchange<Binder>(ref s_defaultBinder, binder, null);
378                 }
379                 return s_defaultBinder;
380             }
381         }
382
383         private static volatile Binder s_defaultBinder;
384
385         public static readonly char Delimiter = '.';
386         public static readonly Type[] EmptyTypes = Array.Empty<Type>();
387         public static readonly object Missing = System.Reflection.Missing.Value;
388
389         public static readonly MemberFilter FilterAttribute = FilterAttributeImpl;
390         public static readonly MemberFilter FilterName = (m, c) => FilterNameImpl(m, c, StringComparison.Ordinal);
391         public static readonly MemberFilter FilterNameIgnoreCase = (m, c) => FilterNameImpl(m, c, StringComparison.OrdinalIgnoreCase);
392
393         private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
394     }
395 }