PropertyInfo.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 class PropertyInfo : MemberInfo
  10. {
  11. protected PropertyInfo() { }
  12. public override MemberTypes MemberType => MemberTypes.Property;
  13. public abstract Type PropertyType { get; }
  14. public abstract ParameterInfo[] GetIndexParameters();
  15. public abstract PropertyAttributes Attributes { get; }
  16. public bool IsSpecialName => (Attributes & PropertyAttributes.SpecialName) != 0;
  17. public abstract bool CanRead { get; }
  18. public abstract bool CanWrite { get; }
  19. public MethodInfo[] GetAccessors() => GetAccessors(nonPublic: false);
  20. public abstract MethodInfo[] GetAccessors(bool nonPublic);
  21. public virtual MethodInfo GetMethod => GetGetMethod(nonPublic: true);
  22. public MethodInfo GetGetMethod() => GetGetMethod(nonPublic: false);
  23. public abstract MethodInfo GetGetMethod(bool nonPublic);
  24. public virtual MethodInfo SetMethod => GetSetMethod(nonPublic: true);
  25. public MethodInfo GetSetMethod() => GetSetMethod(nonPublic: false);
  26. public abstract MethodInfo GetSetMethod(bool nonPublic);
  27. public virtual Type[] GetOptionalCustomModifiers() => Array.Empty<Type>();
  28. public virtual Type[] GetRequiredCustomModifiers() => Array.Empty<Type>();
  29. [DebuggerHidden]
  30. [DebuggerStepThrough]
  31. public object GetValue(object obj) => GetValue(obj, index: null);
  32. [DebuggerHidden]
  33. [DebuggerStepThrough]
  34. public virtual object GetValue(object obj, object[] index) => GetValue(obj, BindingFlags.Default, binder: null, index: index, culture: null);
  35. public abstract object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture);
  36. public virtual object GetConstantValue() { throw NotImplemented.ByDesign; }
  37. public virtual object GetRawConstantValue() { throw NotImplemented.ByDesign; }
  38. [DebuggerHidden]
  39. [DebuggerStepThrough]
  40. public void SetValue(object obj, object value) => SetValue(obj, value, index: null);
  41. [DebuggerHidden]
  42. [DebuggerStepThrough]
  43. public virtual void SetValue(object obj, object value, object[] index) => SetValue(obj, value, BindingFlags.Default, binder: null, index: index, culture: null);
  44. public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture);
  45. public override bool Equals(object obj) => base.Equals(obj);
  46. public override int GetHashCode() => base.GetHashCode();
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public static bool operator ==(PropertyInfo left, PropertyInfo right)
  49. {
  50. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  51. // so it can become a simple test
  52. if (right is null)
  53. {
  54. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  55. return (left is null) ? true : false;
  56. }
  57. // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
  58. if ((object)left == (object)right)
  59. {
  60. return true;
  61. }
  62. return (left is null) ? false : left.Equals(right);
  63. }
  64. public static bool operator !=(PropertyInfo left, PropertyInfo right) => !(left == right);
  65. }
  66. }