MemberInfo.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. namespace System.Reflection
  6. {
  7. public abstract partial class MemberInfo : ICustomAttributeProvider
  8. {
  9. protected MemberInfo() { }
  10. public abstract MemberTypes MemberType { get; }
  11. public abstract string Name { get; }
  12. public abstract Type DeclaringType { get; }
  13. public abstract Type ReflectedType { get; }
  14. public virtual Module Module
  15. {
  16. get
  17. {
  18. // This check is necessary because for some reason, Type adds a new "Module" property that hides the inherited one instead
  19. // of overriding.
  20. Type type = this as Type;
  21. if (type != null)
  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. public static bool operator ==(MemberInfo left, MemberInfo right)
  37. {
  38. if (object.ReferenceEquals(left, right))
  39. return true;
  40. if ((object)left == null || (object)right == null)
  41. return false;
  42. Type type1, type2;
  43. MethodBase method1, method2;
  44. FieldInfo field1, field2;
  45. EventInfo event1, event2;
  46. PropertyInfo property1, property2;
  47. if ((type1 = left as Type) != null && (type2 = right as Type) != null)
  48. return type1 == type2;
  49. else if ((method1 = left as MethodBase) != null && (method2 = right as MethodBase) != null)
  50. return method1 == method2;
  51. else if ((field1 = left as FieldInfo) != null && (field2 = right as FieldInfo) != null)
  52. return field1 == field2;
  53. else if ((event1 = left as EventInfo) != null && (event2 = right as EventInfo) != null)
  54. return event1 == event2;
  55. else if ((property1 = left as PropertyInfo) != null && (property2 = right as PropertyInfo) != null)
  56. return property1 == property2;
  57. return false;
  58. }
  59. public static bool operator !=(MemberInfo left, MemberInfo right) => !(left == right);
  60. }
  61. }