Add IsCollectible property to Memberinfo and MethodInfo (#21155)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / src / System / Reflection / RuntimeFieldInfo.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.Collections.Generic;
6 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
7
8 namespace System.Reflection
9 {
10     internal abstract class RuntimeFieldInfo : FieldInfo
11     {
12         #region Private Data Members
13         private BindingFlags m_bindingFlags;
14         protected RuntimeTypeCache m_reflectedTypeCache;
15         protected RuntimeType m_declaringType;
16         #endregion
17
18         #region Constructor
19         protected RuntimeFieldInfo()
20         {
21             // Used for dummy head node during population
22         }
23         protected RuntimeFieldInfo(RuntimeTypeCache reflectedTypeCache, RuntimeType declaringType, BindingFlags bindingFlags)
24         {
25             m_bindingFlags = bindingFlags;
26             m_declaringType = declaringType;
27             m_reflectedTypeCache = reflectedTypeCache;
28         }
29         #endregion
30
31         #region NonPublic Members
32         internal BindingFlags BindingFlags { get { return m_bindingFlags; } }
33         private RuntimeType ReflectedTypeInternal
34         {
35             get
36             {
37                 return m_reflectedTypeCache.GetRuntimeType();
38             }
39         }
40
41         internal RuntimeType GetDeclaringTypeInternal()
42         {
43             return m_declaringType;
44         }
45
46         internal RuntimeType GetRuntimeType() { return m_declaringType; }
47         internal abstract RuntimeModule GetRuntimeModule();
48         #endregion
49
50         #region MemberInfo Overrides
51         public override MemberTypes MemberType { get { return MemberTypes.Field; } }
52         public override Type ReflectedType
53         {
54             get
55             {
56                 return m_reflectedTypeCache.IsGlobal ? null : ReflectedTypeInternal;
57             }
58         }
59
60         public override Type DeclaringType
61         {
62             get
63             {
64                 return m_reflectedTypeCache.IsGlobal ? null : m_declaringType;
65             }
66         }
67
68         public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeFieldInfo>(other);
69
70         public override Module Module { get { return GetRuntimeModule(); } }
71         public override bool IsCollectible => m_declaringType.IsCollectible;
72         #endregion
73
74         #region Object Overrides
75         public unsafe override string ToString()
76         {
77             return FieldType.FormatTypeName() + " " + Name;
78         }
79         #endregion
80
81         #region ICustomAttributeProvider
82         public override object[] GetCustomAttributes(bool inherit)
83         {
84             return CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType);
85         }
86
87         public override object[] GetCustomAttributes(Type attributeType, bool inherit)
88         {
89             if (attributeType == null)
90                 throw new ArgumentNullException(nameof(attributeType));
91
92             RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
93
94             if (attributeRuntimeType == null)
95                 throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
96
97             return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
98         }
99
100         public override bool IsDefined(Type attributeType, bool inherit)
101         {
102             if (attributeType == null)
103                 throw new ArgumentNullException(nameof(attributeType));
104
105             RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
106
107             if (attributeRuntimeType == null)
108                 throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
109
110             return CustomAttribute.IsDefined(this, attributeRuntimeType);
111         }
112
113         public override IList<CustomAttributeData> GetCustomAttributesData()
114         {
115             return CustomAttributeData.GetCustomAttributesInternal(this);
116         }
117         #endregion
118
119         #region FieldInfo Overrides
120         // All implemented on derived classes
121         #endregion
122     }
123 }