MethodBase.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.Globalization;
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. namespace System.Reflection
  8. {
  9. public abstract partial class MethodBase : MemberInfo
  10. {
  11. protected MethodBase() { }
  12. public abstract ParameterInfo[] GetParameters();
  13. public abstract MethodAttributes Attributes { get; }
  14. public virtual MethodImplAttributes MethodImplementationFlags => GetMethodImplementationFlags();
  15. public abstract MethodImplAttributes GetMethodImplementationFlags();
  16. public virtual MethodBody GetMethodBody() { throw new InvalidOperationException(); }
  17. public virtual CallingConventions CallingConvention => CallingConventions.Standard;
  18. public bool IsAbstract => (Attributes & MethodAttributes.Abstract) != 0;
  19. public bool IsConstructor
  20. {
  21. get
  22. {
  23. // To be backward compatible we only return true for instance RTSpecialName ctors.
  24. return (this is ConstructorInfo &&
  25. !IsStatic &&
  26. ((Attributes & MethodAttributes.RTSpecialName) == MethodAttributes.RTSpecialName));
  27. }
  28. }
  29. public bool IsFinal => (Attributes & MethodAttributes.Final) != 0;
  30. public bool IsHideBySig => (Attributes & MethodAttributes.HideBySig) != 0;
  31. public bool IsSpecialName => (Attributes & MethodAttributes.SpecialName) != 0;
  32. public bool IsStatic => (Attributes & MethodAttributes.Static) != 0;
  33. public bool IsVirtual => (Attributes & MethodAttributes.Virtual) != 0;
  34. public bool IsAssembly => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
  35. public bool IsFamily => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
  36. public bool IsFamilyAndAssembly => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
  37. public bool IsFamilyOrAssembly => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
  38. public bool IsPrivate => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
  39. public bool IsPublic => (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
  40. public virtual bool IsConstructedGenericMethod => IsGenericMethod && !IsGenericMethodDefinition;
  41. public virtual bool IsGenericMethod => false;
  42. public virtual bool IsGenericMethodDefinition => false;
  43. public virtual Type[] GetGenericArguments() { throw new NotSupportedException(SR.NotSupported_SubclassOverride); }
  44. public virtual bool ContainsGenericParameters => false;
  45. [DebuggerHidden]
  46. [DebuggerStepThrough]
  47. public object Invoke(object obj, object[] parameters) => Invoke(obj, BindingFlags.Default, binder: null, parameters: parameters, culture: null);
  48. public abstract object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture);
  49. public abstract RuntimeMethodHandle MethodHandle { get; }
  50. public virtual bool IsSecurityCritical { get { throw NotImplemented.ByDesign; } }
  51. public virtual bool IsSecuritySafeCritical { get { throw NotImplemented.ByDesign; } }
  52. public virtual bool IsSecurityTransparent { get { throw NotImplemented.ByDesign; } }
  53. public override bool Equals(object obj) => base.Equals(obj);
  54. public override int GetHashCode() => base.GetHashCode();
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public static bool operator ==(MethodBase left, MethodBase right)
  57. {
  58. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  59. // so it can become a simple test
  60. if (right is null)
  61. {
  62. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  63. return (left is null) ? true : false;
  64. }
  65. // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
  66. if ((object)left == (object)right)
  67. {
  68. return true;
  69. }
  70. return (left is null) ? false : left.Equals(right);
  71. }
  72. public static bool operator !=(MethodBase left, MethodBase right) => !(left == right);
  73. }
  74. }