FieldInfo.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.Runtime.CompilerServices;
  7. namespace System.Reflection
  8. {
  9. public abstract partial class FieldInfo : MemberInfo
  10. {
  11. protected FieldInfo() { }
  12. public override MemberTypes MemberType => MemberTypes.Field;
  13. public abstract FieldAttributes Attributes { get; }
  14. public abstract Type FieldType { get; }
  15. public bool IsInitOnly => (Attributes & FieldAttributes.InitOnly) != 0;
  16. public bool IsLiteral => (Attributes & FieldAttributes.Literal) != 0;
  17. public bool IsNotSerialized => (Attributes & FieldAttributes.NotSerialized) != 0;
  18. public bool IsPinvokeImpl => (Attributes & FieldAttributes.PinvokeImpl) != 0;
  19. public bool IsSpecialName => (Attributes & FieldAttributes.SpecialName) != 0;
  20. public bool IsStatic => (Attributes & FieldAttributes.Static) != 0;
  21. public bool IsAssembly => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
  22. public bool IsFamily => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
  23. public bool IsFamilyAndAssembly => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
  24. public bool IsFamilyOrAssembly => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
  25. public bool IsPrivate => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
  26. public bool IsPublic => (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
  27. public virtual bool IsSecurityCritical => true;
  28. public virtual bool IsSecuritySafeCritical => false;
  29. public virtual bool IsSecurityTransparent => false;
  30. public abstract RuntimeFieldHandle FieldHandle { get; }
  31. public override bool Equals(object obj) => base.Equals(obj);
  32. public override int GetHashCode() => base.GetHashCode();
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public static bool operator ==(FieldInfo left, FieldInfo right)
  35. {
  36. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  37. // so it can become a simple test
  38. if (right is null)
  39. {
  40. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  41. return (left is null) ? true : false;
  42. }
  43. // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
  44. if ((object)left == (object)right)
  45. {
  46. return true;
  47. }
  48. return (left is null) ? false : left.Equals(right);
  49. }
  50. public static bool operator !=(FieldInfo left, FieldInfo right) => !(left == right);
  51. public abstract object GetValue(object obj);
  52. [DebuggerHidden]
  53. [DebuggerStepThrough]
  54. public void SetValue(object obj, object value) => SetValue(obj, value, BindingFlags.Default, Type.DefaultBinder, null);
  55. public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
  56. [CLSCompliant(false)]
  57. public virtual void SetValueDirect(TypedReference obj, object value) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }
  58. [CLSCompliant(false)]
  59. public virtual object GetValueDirect(TypedReference obj) { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }
  60. public virtual object GetRawConstantValue() { throw new NotSupportedException(SR.NotSupported_AbstractNonCLS); }
  61. public virtual Type[] GetOptionalCustomModifiers() { throw NotImplemented.ByDesign; }
  62. public virtual Type[] GetRequiredCustomModifiers() { throw NotImplemented.ByDesign; }
  63. }
  64. }