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.
5 using System.Collections.Generic;
6 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
8 namespace System.Reflection
10 internal abstract class RuntimeFieldInfo : FieldInfo
12 #region Private Data Members
13 private BindingFlags m_bindingFlags;
14 protected RuntimeTypeCache m_reflectedTypeCache;
15 protected RuntimeType m_declaringType;
19 protected RuntimeFieldInfo()
21 // Used for dummy head node during population
23 protected RuntimeFieldInfo(RuntimeTypeCache reflectedTypeCache, RuntimeType declaringType, BindingFlags bindingFlags)
25 m_bindingFlags = bindingFlags;
26 m_declaringType = declaringType;
27 m_reflectedTypeCache = reflectedTypeCache;
31 #region NonPublic Members
32 internal BindingFlags BindingFlags { get { return m_bindingFlags; } }
33 private RuntimeType ReflectedTypeInternal
37 return m_reflectedTypeCache.GetRuntimeType();
41 internal RuntimeType GetDeclaringTypeInternal()
43 return m_declaringType;
46 internal RuntimeType GetRuntimeType() { return m_declaringType; }
47 internal abstract RuntimeModule GetRuntimeModule();
50 #region MemberInfo Overrides
51 public override MemberTypes MemberType { get { return MemberTypes.Field; } }
52 public override Type ReflectedType
56 return m_reflectedTypeCache.IsGlobal ? null : ReflectedTypeInternal;
60 public override Type DeclaringType
64 return m_reflectedTypeCache.IsGlobal ? null : m_declaringType;
68 public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeFieldInfo>(other);
70 public override Module Module { get { return GetRuntimeModule(); } }
71 public override bool IsCollectible => m_declaringType.IsCollectible;
74 #region Object Overrides
75 public unsafe override string ToString()
77 return FieldType.FormatTypeName() + " " + Name;
81 #region ICustomAttributeProvider
82 public override object[] GetCustomAttributes(bool inherit)
84 return CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType);
87 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
89 if (attributeType == null)
90 throw new ArgumentNullException(nameof(attributeType));
92 RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
94 if (attributeRuntimeType == null)
95 throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
97 return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
100 public override bool IsDefined(Type attributeType, bool inherit)
102 if (attributeType == null)
103 throw new ArgumentNullException(nameof(attributeType));
105 RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
107 if (attributeRuntimeType == null)
108 throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
110 return CustomAttribute.IsDefined(this, attributeRuntimeType);
113 public override IList<CustomAttributeData> GetCustomAttributesData()
115 return CustomAttributeData.GetCustomAttributesInternal(this);
119 #region FieldInfo Overrides
120 // All implemented on derived classes