MemberInfo.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. Type type = this as Type;
  22. if (type != null)
  23. return type.Module;
  24. throw NotImplemented.ByDesign;
  25. }
  26. }
  27. public virtual bool HasSameMetadataDefinitionAs(MemberInfo other) { throw NotImplemented.ByDesign; }
  28. public abstract bool IsDefined(Type attributeType, bool inherit);
  29. public abstract object[] GetCustomAttributes(bool inherit);
  30. public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);
  31. public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData();
  32. public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
  33. public virtual bool IsCollectible => true;
  34. public virtual int MetadataToken { get { throw new InvalidOperationException(); } }
  35. public override bool Equals(object obj) => base.Equals(obj);
  36. public override int GetHashCode() => base.GetHashCode();
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public static bool operator ==(MemberInfo left, MemberInfo right)
  39. {
  40. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  41. // so it can become a simple test
  42. if (right is null)
  43. {
  44. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  45. return (left is null) ? true : false;
  46. }
  47. // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
  48. if ((object)left == (object)right)
  49. {
  50. return true;
  51. }
  52. return (left is null) ? false : left.Equals(right);
  53. }
  54. public static bool operator !=(MemberInfo left, MemberInfo right) => !(left == right);
  55. }
  56. }