3 // Copyright(c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
9 namespace System.Reflection
12 using System.Collections.Generic;
13 using System.Diagnostics;
14 using System.Diagnostics.Contracts;
15 using System.Globalization;
17 using System.Runtime.CompilerServices;
18 using System.Runtime.ConstrainedExecution;
19 using System.Runtime.InteropServices;
21 using System.Runtime.Remoting.Metadata;
22 #endif //FEATURE_REMOTING
23 using System.Runtime.Serialization;
24 using System.Security.Permissions;
25 using System.Threading;
26 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
29 [ClassInterface(ClassInterfaceType.None)]
30 [ComDefaultInterface(typeof(_FieldInfo))]
31 #pragma warning disable 618
32 [PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
33 #pragma warning restore 618
34 [System.Runtime.InteropServices.ComVisible(true)]
35 public abstract class FieldInfo : MemberInfo, _FieldInfo
37 #region Static Members
38 public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle)
40 if (handle.IsNullHandle())
41 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
43 FieldInfo f = RuntimeType.GetFieldInfo(handle.GetRuntimeFieldInfo());
45 Type declaringType = f.DeclaringType;
46 if (declaringType != null && declaringType.IsGenericType)
47 throw new ArgumentException(String.Format(
48 CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_FieldDeclaringTypeGeneric"),
49 f.Name, declaringType.GetGenericTypeDefinition()));
54 [System.Runtime.InteropServices.ComVisible(false)]
55 public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
57 if (handle.IsNullHandle())
58 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
60 return RuntimeType.GetFieldInfo(declaringType.GetRuntimeType(), handle.GetRuntimeFieldInfo());
65 protected FieldInfo() { }
69 public static bool operator ==(FieldInfo left, FieldInfo right)
71 if (ReferenceEquals(left, right))
74 if ((object)left == null || (object)right == null ||
75 left is RuntimeFieldInfo || right is RuntimeFieldInfo)
79 return left.Equals(right);
82 public static bool operator !=(FieldInfo left, FieldInfo right)
84 return !(left == right);
86 #endif // !FEATURE_CORECLR
88 public override bool Equals(object obj)
90 return base.Equals(obj);
93 public override int GetHashCode()
95 return base.GetHashCode();
98 #region MemberInfo Overrides
99 public override MemberTypes MemberType { get { return System.Reflection.MemberTypes.Field; } }
102 #region Public Abstract\Virtual Members
104 public virtual Type[] GetRequiredCustomModifiers()
106 throw new NotImplementedException();
109 public virtual Type[] GetOptionalCustomModifiers()
111 throw new NotImplementedException();
114 [CLSCompliant(false)]
115 public virtual void SetValueDirect(TypedReference obj, Object value)
117 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
120 [CLSCompliant(false)]
121 public virtual Object GetValueDirect(TypedReference obj)
123 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
126 public abstract RuntimeFieldHandle FieldHandle { get; }
128 public abstract Type FieldType { get; }
130 public abstract Object GetValue(Object obj);
132 public virtual Object GetRawConstantValue() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS")); }
134 public abstract void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
136 public abstract FieldAttributes Attributes { get; }
139 #region Public Members
140 [DebuggerStepThroughAttribute]
141 [Diagnostics.DebuggerHidden]
142 public void SetValue(Object obj, Object value)
144 // Theoretically we should set up a LookForMyCaller stack mark here and pass that along.
145 // But to maintain backward compatibility we can't switch to calling an
146 // internal overload that takes a stack mark.
147 // Fortunately the stack walker skips all the reflection invocation frames including this one.
148 // So this method will never be returned by the stack walker as the caller.
149 // See SystemDomain::CallersMethodCallbackWithStackMark in AppDomain.cpp.
150 SetValue(obj, value, BindingFlags.Default, Type.DefaultBinder, null);
153 public bool IsPublic { get { return(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public; } }
155 public bool IsPrivate { get { return(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private; } }
157 public bool IsFamily { get { return(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family; } }
159 public bool IsAssembly { get { return(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly; } }
161 public bool IsFamilyAndAssembly { get { return(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem; } }
163 public bool IsFamilyOrAssembly { get { return(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem; } }
165 public bool IsStatic { get { return(Attributes & FieldAttributes.Static) != 0; } }
167 public bool IsInitOnly { get { return(Attributes & FieldAttributes.InitOnly) != 0; } }
169 public bool IsLiteral { get { return(Attributes & FieldAttributes.Literal) != 0; } }
171 public bool IsNotSerialized { get { return(Attributes & FieldAttributes.NotSerialized) != 0; } }
173 public bool IsSpecialName { get { return(Attributes & FieldAttributes.SpecialName) != 0; } }
175 public bool IsPinvokeImpl { get { return(Attributes & FieldAttributes.PinvokeImpl) != 0; } }
177 public virtual bool IsSecurityCritical
179 get { return FieldHandle.IsSecurityCritical(); }
182 public virtual bool IsSecuritySafeCritical
184 get { return FieldHandle.IsSecuritySafeCritical(); }
187 public virtual bool IsSecurityTransparent
189 get { return FieldHandle.IsSecurityTransparent(); }
195 Type _FieldInfo.GetType()
197 return base.GetType();
200 void _FieldInfo.GetTypeInfoCount(out uint pcTInfo)
202 throw new NotImplementedException();
205 void _FieldInfo.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
207 throw new NotImplementedException();
210 void _FieldInfo.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
212 throw new NotImplementedException();
215 // If you implement this method, make sure to include _FieldInfo.Invoke in VM\DangerousAPIs.h and
216 // include _FieldInfo in SystemDomain::IsReflectionInvocationMethod in AppDomain.cpp.
217 void _FieldInfo.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
219 throw new NotImplementedException();
225 internal abstract class RuntimeFieldInfo : FieldInfo, ISerializable
227 #region Private Data Members
228 private BindingFlags m_bindingFlags;
229 protected RuntimeTypeCache m_reflectedTypeCache;
230 protected RuntimeType m_declaringType;
234 protected RuntimeFieldInfo()
236 // Used for dummy head node during population
238 protected RuntimeFieldInfo(RuntimeTypeCache reflectedTypeCache, RuntimeType declaringType, BindingFlags bindingFlags)
240 m_bindingFlags = bindingFlags;
241 m_declaringType = declaringType;
242 m_reflectedTypeCache = reflectedTypeCache;
247 #region Legacy Remoting Cache
248 // The size of CachedData is accounted for by BaseObjectWithCachedData in object.h.
249 // This member is currently being used by Remoting for caching remoting data. If you
250 // need to cache data here, talk to the Remoting team to work out a mechanism, so that
251 // both caching systems can happily work together.
252 private RemotingFieldCachedData m_cachedData;
254 internal RemotingFieldCachedData RemotingCache
258 // This grabs an internal copy of m_cachedData and uses
259 // that instead of looking at m_cachedData directly because
260 // the cache may get cleared asynchronously. This prevents
261 // us from having to take a lock.
262 RemotingFieldCachedData cache = m_cachedData;
265 cache = new RemotingFieldCachedData(this);
266 RemotingFieldCachedData ret = Interlocked.CompareExchange(ref m_cachedData, cache, null);
274 #endif //FEATURE_REMOTING
276 #region NonPublic Members
277 internal BindingFlags BindingFlags { get { return m_bindingFlags; } }
278 private RuntimeType ReflectedTypeInternal
282 return m_reflectedTypeCache.GetRuntimeType();
286 internal RuntimeType GetDeclaringTypeInternal()
288 return m_declaringType;
291 internal RuntimeType GetRuntimeType() { return m_declaringType; }
292 internal abstract RuntimeModule GetRuntimeModule();
295 #region MemberInfo Overrides
296 public override MemberTypes MemberType { get { return MemberTypes.Field; } }
297 public override Type ReflectedType
301 return m_reflectedTypeCache.IsGlobal ? null : ReflectedTypeInternal;
305 public override Type DeclaringType
309 return m_reflectedTypeCache.IsGlobal ? null : m_declaringType;
313 public override Module Module { get { return GetRuntimeModule(); } }
316 #region Object Overrides
317 public unsafe override String ToString()
319 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
320 return FieldType.ToString() + " " + Name;
322 return FieldType.FormatTypeName() + " " + Name;
326 #region ICustomAttributeProvider
327 public override Object[] GetCustomAttributes(bool inherit)
329 return CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType);
332 public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
334 if (attributeType == null)
335 throw new ArgumentNullException("attributeType");
336 Contract.EndContractBlock();
338 RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
340 if (attributeRuntimeType == null)
341 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"attributeType");
343 return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
346 [System.Security.SecuritySafeCritical] // auto-generated
347 public override bool IsDefined(Type attributeType, bool inherit)
349 if (attributeType == null)
350 throw new ArgumentNullException("attributeType");
351 Contract.EndContractBlock();
353 RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;
355 if (attributeRuntimeType == null)
356 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"attributeType");
358 return CustomAttribute.IsDefined(this, attributeRuntimeType);
361 public override IList<CustomAttributeData> GetCustomAttributesData()
363 return CustomAttributeData.GetCustomAttributesInternal(this);
367 #region FieldInfo Overrides
368 // All implemented on derived classes
371 #region ISerializable Implementation
372 [System.Security.SecurityCritical] // auto-generated
373 public void GetObjectData(SerializationInfo info, StreamingContext context)
376 throw new ArgumentNullException("info");
377 Contract.EndContractBlock();
378 MemberInfoSerializationHolder.GetSerializationInfo(
381 ReflectedTypeInternal,
389 internal unsafe sealed class RtFieldInfo : RuntimeFieldInfo, IRuntimeFieldInfo
392 [System.Security.SecurityCritical] // auto-generated
393 [System.Runtime.Versioning.ResourceExposure(System.Runtime.Versioning.ResourceScope.None)]
394 [MethodImplAttribute(MethodImplOptions.InternalCall)]
395 static private extern void PerformVisibilityCheckOnField(IntPtr field, Object target, RuntimeType declaringType, FieldAttributes attr, uint invocationFlags);
398 #region Private Data Members
400 private IntPtr m_fieldHandle;
401 private FieldAttributes m_fieldAttributes;
403 private string m_name;
404 private RuntimeType m_fieldType;
405 private INVOCATION_FLAGS m_invocationFlags;
408 private bool IsNonW8PFrameworkAPI()
410 if (GetRuntimeType().IsNonW8PFrameworkAPI())
414 if (m_declaringType.IsEnum)
417 RuntimeAssembly rtAssembly = GetRuntimeAssembly();
418 if (rtAssembly.IsFrameworkAssembly())
420 int ctorToken = rtAssembly.InvocableAttributeCtorToken;
421 if (System.Reflection.MetadataToken.IsNullToken(ctorToken) ||
422 !CustomAttribute.IsAttributeDefined(GetRuntimeModule(), MetadataToken, ctorToken))
430 internal INVOCATION_FLAGS InvocationFlags
434 if ((m_invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_INITIALIZED) == 0)
436 Type declaringType = DeclaringType;
437 bool fIsReflectionOnlyType = (declaringType is ReflectionOnlyType);
439 INVOCATION_FLAGS invocationFlags = 0;
441 // first take care of all the NO_INVOKE cases
443 (declaringType != null && declaringType.ContainsGenericParameters) ||
444 (declaringType == null && Module.Assembly.ReflectionOnly) ||
445 (fIsReflectionOnlyType)
448 invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE;
451 // If the invocationFlags are still 0, then
452 // this should be an usable field, determine the other flags
453 if (invocationFlags == 0)
455 if ((m_fieldAttributes & FieldAttributes.InitOnly) != (FieldAttributes)0)
456 invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_SPECIAL_FIELD;
458 if ((m_fieldAttributes & FieldAttributes.HasFieldRVA) != (FieldAttributes)0)
459 invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_SPECIAL_FIELD;
461 // A public field is inaccesible to Transparent code if the field is Critical.
462 bool needsTransparencySecurityCheck = IsSecurityCritical && !IsSecuritySafeCritical;
463 bool needsVisibilitySecurityCheck = ((m_fieldAttributes & FieldAttributes.FieldAccessMask) != FieldAttributes.Public) ||
464 (declaringType != null && declaringType.NeedsReflectionSecurityCheck);
465 if (needsTransparencySecurityCheck || needsVisibilitySecurityCheck)
466 invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY;
468 // find out if the field type is one of the following: Primitive, Enum or Pointer
469 Type fieldType = FieldType;
470 if (fieldType.IsPointer || fieldType.IsEnum || fieldType.IsPrimitive)
471 invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_FIELD_SPECIAL_CAST;
475 if (AppDomain.ProfileAPICheck && IsNonW8PFrameworkAPI())
476 invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API;
477 #endif // FEATURE_APPX
479 // must be last to avoid threading problems
480 m_invocationFlags = invocationFlags | INVOCATION_FLAGS.INVOCATION_FLAGS_INITIALIZED;
483 return m_invocationFlags;
488 private RuntimeAssembly GetRuntimeAssembly() { return m_declaringType.GetRuntimeAssembly(); }
491 [System.Security.SecurityCritical] // auto-generated
492 internal RtFieldInfo(
493 RuntimeFieldHandleInternal handle, RuntimeType declaringType, RuntimeTypeCache reflectedTypeCache, BindingFlags bindingFlags)
494 : base(reflectedTypeCache, declaringType, bindingFlags)
496 m_fieldHandle = handle.Value;
497 m_fieldAttributes = RuntimeFieldHandle.GetAttributes(handle);
501 #region Private Members
502 RuntimeFieldHandleInternal IRuntimeFieldInfo.Value
504 [System.Security.SecuritySafeCritical]
507 return new RuntimeFieldHandleInternal(m_fieldHandle);
513 #region Internal Members
514 internal void CheckConsistency(Object target)
516 // only test instance fields
517 if ((m_fieldAttributes & FieldAttributes.Static) != FieldAttributes.Static)
519 if (!m_declaringType.IsInstanceOfType(target))
523 #if FEATURE_LEGACYNETCF
524 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
525 throw new ArgumentNullException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
528 throw new TargetException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
532 throw new ArgumentException(
533 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_FieldDeclTarget"),
534 Name, m_declaringType, target.GetType()));
540 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
541 internal override bool CacheEquals(object o)
543 RtFieldInfo m = o as RtFieldInfo;
545 if ((object)m == null)
548 return m.m_fieldHandle == m_fieldHandle;
551 [System.Security.SecurityCritical]
552 [DebuggerStepThroughAttribute]
553 [Diagnostics.DebuggerHidden]
554 internal void InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, ref StackCrawlMark stackMark)
556 INVOCATION_FLAGS invocationFlags = InvocationFlags;
557 RuntimeType declaringType = DeclaringType as RuntimeType;
559 if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE) != 0)
561 if (declaringType != null && declaringType.ContainsGenericParameters)
562 throw new InvalidOperationException(Environment.GetResourceString("Arg_UnboundGenField"));
564 if ((declaringType == null && Module.Assembly.ReflectionOnly) || declaringType is ReflectionOnlyType)
565 throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyField"));
567 throw new FieldAccessException();
570 CheckConsistency(obj);
572 RuntimeType fieldType = (RuntimeType)FieldType;
573 value = fieldType.CheckValue(value, binder, culture, invokeAttr);
575 #region Security Check
577 if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0)
579 RuntimeAssembly caller = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
580 if (caller != null && !caller.IsSafeForReflection())
581 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", FullName));
585 if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_SPECIAL_FIELD | INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY)) != 0)
586 PerformVisibilityCheckOnField(m_fieldHandle, obj, m_declaringType, m_fieldAttributes, (uint)m_invocationFlags);
589 bool domainInitialized = false;
590 if (declaringType == null)
592 RuntimeFieldHandle.SetValue(this, obj, value, fieldType, m_fieldAttributes, null, ref domainInitialized);
596 domainInitialized = declaringType.DomainInitialized;
597 RuntimeFieldHandle.SetValue(this, obj, value, fieldType, m_fieldAttributes, declaringType, ref domainInitialized);
598 declaringType.DomainInitialized = domainInitialized;
602 // UnsafeSetValue doesn't perform any consistency or visibility check.
603 // It is the caller's responsibility to ensure the operation is safe.
604 // When the caller needs to perform visibility checks they should call
605 // InternalSetValue() instead. When the caller needs to perform
606 // consistency checks they should call CheckConsistency() before
607 // calling this method.
608 [System.Security.SecurityCritical] // auto-generated
609 [DebuggerStepThroughAttribute]
610 [Diagnostics.DebuggerHidden]
611 internal void UnsafeSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
613 RuntimeType declaringType = DeclaringType as RuntimeType;
614 RuntimeType fieldType = (RuntimeType)FieldType;
615 value = fieldType.CheckValue(value, binder, culture, invokeAttr);
617 bool domainInitialized = false;
618 if (declaringType == null)
620 RuntimeFieldHandle.SetValue(this, obj, value, fieldType, m_fieldAttributes, null, ref domainInitialized);
624 domainInitialized = declaringType.DomainInitialized;
625 RuntimeFieldHandle.SetValue(this, obj, value, fieldType, m_fieldAttributes, declaringType, ref domainInitialized);
626 declaringType.DomainInitialized = domainInitialized;
630 [System.Security.SecuritySafeCritical]
631 [DebuggerStepThroughAttribute]
632 [Diagnostics.DebuggerHidden]
633 internal Object InternalGetValue(Object obj, ref StackCrawlMark stackMark)
635 INVOCATION_FLAGS invocationFlags = InvocationFlags;
636 RuntimeType declaringType = DeclaringType as RuntimeType;
638 if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE) != 0)
640 if (declaringType != null && DeclaringType.ContainsGenericParameters)
641 throw new InvalidOperationException(Environment.GetResourceString("Arg_UnboundGenField"));
643 if ((declaringType == null && Module.Assembly.ReflectionOnly) || declaringType is ReflectionOnlyType)
644 throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyField"));
646 throw new FieldAccessException();
649 CheckConsistency(obj);
652 if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0)
654 RuntimeAssembly caller = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
655 if (caller != null && !caller.IsSafeForReflection())
656 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", FullName));
660 RuntimeType fieldType = (RuntimeType)FieldType;
661 if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY) != 0)
662 PerformVisibilityCheckOnField(m_fieldHandle, obj, m_declaringType, m_fieldAttributes, (uint)(m_invocationFlags & ~INVOCATION_FLAGS.INVOCATION_FLAGS_SPECIAL_FIELD));
664 return UnsafeGetValue(obj);
667 // UnsafeGetValue doesn't perform any consistency or visibility check.
668 // It is the caller's responsibility to ensure the operation is safe.
669 // When the caller needs to perform visibility checks they should call
670 // InternalGetValue() instead. When the caller needs to perform
671 // consistency checks they should call CheckConsistency() before
672 // calling this method.
673 [System.Security.SecurityCritical]
674 [DebuggerStepThroughAttribute]
675 [Diagnostics.DebuggerHidden]
676 internal Object UnsafeGetValue(Object obj)
678 RuntimeType declaringType = DeclaringType as RuntimeType;
680 RuntimeType fieldType = (RuntimeType)FieldType;
682 bool domainInitialized = false;
683 if (declaringType == null)
685 return RuntimeFieldHandle.GetValue(this, obj, fieldType, null, ref domainInitialized);
689 domainInitialized = declaringType.DomainInitialized;
690 object retVal = RuntimeFieldHandle.GetValue(this, obj, fieldType, declaringType, ref domainInitialized);
691 declaringType.DomainInitialized = domainInitialized;
698 #region MemberInfo Overrides
699 public override String Name
701 [System.Security.SecuritySafeCritical] // auto-generated
705 m_name = RuntimeFieldHandle.GetName(this);
711 internal String FullName
715 return String.Format("{0}.{1}", DeclaringType.FullName, Name);
719 public override int MetadataToken
721 [System.Security.SecuritySafeCritical] // auto-generated
722 get { return RuntimeFieldHandle.GetToken(this); }
725 [System.Security.SecuritySafeCritical] // auto-generated
726 internal override RuntimeModule GetRuntimeModule()
728 return RuntimeTypeHandle.GetModule(RuntimeFieldHandle.GetApproxDeclaringType(this));
733 #region FieldInfo Overrides
734 public override Object GetValue(Object obj)
736 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
737 return InternalGetValue(obj, ref stackMark);
740 public override object GetRawConstantValue() { throw new InvalidOperationException(); }
742 [System.Security.SecuritySafeCritical] // auto-generated
743 [DebuggerStepThroughAttribute]
744 [Diagnostics.DebuggerHidden]
745 public override Object GetValueDirect(TypedReference obj)
748 throw new ArgumentException(Environment.GetResourceString("Arg_TypedReference_Null"));
749 Contract.EndContractBlock();
753 // Passing TypedReference by reference is easier to make correct in native code
754 return RuntimeFieldHandle.GetValueDirect(this, (RuntimeType)FieldType, &obj, (RuntimeType)DeclaringType);
758 [System.Security.SecuritySafeCritical] // auto-generated
759 [DebuggerStepThroughAttribute]
760 [Diagnostics.DebuggerHidden]
761 public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
763 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
764 InternalSetValue(obj, value, invokeAttr, binder, culture, ref stackMark);
767 [System.Security.SecuritySafeCritical] // auto-generated
768 [DebuggerStepThroughAttribute]
769 [Diagnostics.DebuggerHidden]
770 public override void SetValueDirect(TypedReference obj, Object value)
773 throw new ArgumentException(Environment.GetResourceString("Arg_TypedReference_Null"));
774 Contract.EndContractBlock();
778 // Passing TypedReference by reference is easier to make correct in native code
779 RuntimeFieldHandle.SetValueDirect(this, (RuntimeType)FieldType, &obj, value, (RuntimeType)DeclaringType);
783 public override RuntimeFieldHandle FieldHandle
787 Type declaringType = DeclaringType;
788 if ((declaringType == null && Module.Assembly.ReflectionOnly) || declaringType is ReflectionOnlyType)
789 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAllowedInReflectionOnly"));
790 return new RuntimeFieldHandle(this);
794 internal IntPtr GetFieldHandle()
796 return m_fieldHandle;
799 public override FieldAttributes Attributes
803 return m_fieldAttributes;
807 public override Type FieldType
809 [System.Security.SecuritySafeCritical] // auto-generated
812 if (m_fieldType == null)
813 m_fieldType = new Signature(this, m_declaringType).FieldType;
819 [System.Security.SecuritySafeCritical] // auto-generated
820 public override Type[] GetRequiredCustomModifiers()
822 return new Signature(this, m_declaringType).GetCustomModifiers(1, true);
825 [System.Security.SecuritySafeCritical] // auto-generated
826 public override Type[] GetOptionalCustomModifiers()
828 return new Signature(this, m_declaringType).GetCustomModifiers(1, false);
835 internal sealed unsafe class MdFieldInfo : RuntimeFieldInfo, ISerializable
837 #region Private Data Members
838 private int m_tkField;
839 private string m_name;
840 private RuntimeType m_fieldType;
841 private FieldAttributes m_fieldAttributes;
845 internal MdFieldInfo(
846 int tkField, FieldAttributes fieldAttributes, RuntimeTypeHandle declaringTypeHandle, RuntimeTypeCache reflectedTypeCache, BindingFlags bindingFlags)
847 : base(reflectedTypeCache, declaringTypeHandle.GetRuntimeType(), bindingFlags)
851 m_fieldAttributes = fieldAttributes;
855 #region Internal Members
856 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
857 internal override bool CacheEquals(object o)
859 MdFieldInfo m = o as MdFieldInfo;
861 if ((object)m == null)
864 return m.m_tkField == m_tkField &&
865 m_declaringType.GetTypeHandleInternal().GetModuleHandle().Equals(
866 m.m_declaringType.GetTypeHandleInternal().GetModuleHandle());
870 #region MemberInfo Overrides
871 public override String Name
873 [System.Security.SecuritySafeCritical] // auto-generated
877 m_name = GetRuntimeModule().MetadataImport.GetName(m_tkField).ToString();
883 public override int MetadataToken { get { return m_tkField; } }
884 internal override RuntimeModule GetRuntimeModule() { return m_declaringType.GetRuntimeModule(); }
887 #region FieldInfo Overrides
888 public override RuntimeFieldHandle FieldHandle { get { throw new NotSupportedException(); } }
889 public override FieldAttributes Attributes { get { return m_fieldAttributes; } }
891 public override bool IsSecurityCritical { get { return DeclaringType.IsSecurityCritical; } }
892 public override bool IsSecuritySafeCritical { get { return DeclaringType.IsSecuritySafeCritical; } }
893 public override bool IsSecurityTransparent { get { return DeclaringType.IsSecurityTransparent; } }
895 [DebuggerStepThroughAttribute]
896 [Diagnostics.DebuggerHidden]
897 public override Object GetValueDirect(TypedReference obj)
899 return GetValue(null);
902 [DebuggerStepThroughAttribute]
903 [Diagnostics.DebuggerHidden]
904 public override void SetValueDirect(TypedReference obj,Object value)
906 throw new FieldAccessException(Environment.GetResourceString("Acc_ReadOnly"));
909 [DebuggerStepThroughAttribute]
910 [Diagnostics.DebuggerHidden]
911 public unsafe override Object GetValue(Object obj)
913 return GetValue(false);
916 public unsafe override Object GetRawConstantValue() { return GetValue(true); }
918 [System.Security.SecuritySafeCritical] // auto-generated
919 private unsafe Object GetValue(bool raw)
921 // Cannot cache these because they could be user defined non-agile enumerations
923 Object value = MdConstant.GetValue(GetRuntimeModule().MetadataImport, m_tkField, FieldType.GetTypeHandleInternal(), raw);
925 if (value == DBNull.Value)
926 throw new NotSupportedException(Environment.GetResourceString("Arg_EnumLitValueNotFound"));
931 [DebuggerStepThroughAttribute]
932 [Diagnostics.DebuggerHidden]
933 public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
935 throw new FieldAccessException(Environment.GetResourceString("Acc_ReadOnly"));
938 public override Type FieldType
940 [System.Security.SecuritySafeCritical] // auto-generated
943 if (m_fieldType == null)
945 ConstArray fieldMarshal = GetRuntimeModule().MetadataImport.GetSigOfFieldDef(m_tkField);
947 m_fieldType = new Signature(fieldMarshal.Signature.ToPointer(),
948 (int)fieldMarshal.Length, m_declaringType).FieldType;
955 public override Type[] GetRequiredCustomModifiers()
957 return EmptyArray<Type>.Value;
960 public override Type[] GetOptionalCustomModifiers()
962 return EmptyArray<Type>.Value;