MemberInfo.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. namespace System.Reflection
  7. {
  8. public abstract partial class MemberInfo : ICustomAttributeProvider
  9. {
  10. protected MemberInfo() { }
  11. public abstract MemberTypes MemberType { get; }
  12. public abstract string Name { get; }
  13. public abstract Type? DeclaringType { get; }
  14. public abstract Type? ReflectedType { get; }
  15. public virtual Module Module
  16. {
  17. get
  18. {
  19. // This check is necessary because for some reason, Type adds a new "Module" property that hides the inherited one instead
  20. // of overriding.
  21. if (this is Type type)
  22. return type.Module;
  23. throw NotImplemented.ByDesign;
  24. }
  25. }
  26. public virtual bool HasSameMetadataDefinitionAs(MemberInfo other) { throw NotImplemented.ByDesign; }
  27. public abstract bool IsDefined(Type attributeType, bool inherit);
  28. public abstract object[] GetCustomAttributes(bool inherit);
  29. public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);
  30. public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData();
  31. public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
  32. public virtual bool IsCollectible => true;
  33. public virtual int MetadataToken { get { throw new InvalidOperationException(); } }
  34. public override bool Equals(object? obj) => base.Equals(obj);
  35. public override int GetHashCode() => base.GetHashCode();
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static bool operator ==(MemberInfo? left, MemberInfo? right)
  38. {
  39. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  40. // so it can become a simple test
  41. if (right is null)
  42. {
  43. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  44. return (left is null) ? true : false;
  45. }
  46. // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
  47. if ((object?)left == (object)right)
  48. {
  49. return true;
  50. }
  51. return (left is null) ? false : left.Equals(right);
  52. }
  53. public static bool operator !=(MemberInfo? left, MemberInfo? right) => !(left == right);
  54. }
  55. }