Expand Minimize
This topic has not yet been rated - Rate this topic

MethodAttributes Enumeration

April 12, 2014

Specifies flags for method attributes. These flags are defined in the corhdr.h file.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)
[FlagsAttribute]
public enum MethodAttributes
Member nameDescription
AbstractIndicates that the class does not provide an implementation of this method.
AssemblyIndicates that the method is accessible to any class of this assembly.
CheckAccessOnOverrideIndicates that the method can only be overridden when it is also accessible.
FamANDAssemIndicates that the method is accessible to members of this type and its derived types that are in this assembly only.
FamilyIndicates that the method is accessible only to members of this class and its derived classes.
FamORAssemIndicates that the method is accessible to derived classes anywhere, as well as to any class in the assembly.
FinalIndicates that the method cannot be overridden.
HasSecurityIndicates that the method has security associated with it. Reserved flag for runtime use only.
HideBySigIndicates that the method hides by name and signature; otherwise, by name only.
MemberAccessMaskRetrieves accessibility information.
NewSlotIndicates that the method always gets a new slot in the vtable.
PinvokeImplIndicates that the method implementation is forwarded through PInvoke (Platform Invocation Services).
PrivateIndicates that the method is accessible only to the current class.
PrivateScopeIndicates that the member cannot be referenced.
PublicIndicates that the method is accessible to any object for which this object is in scope.
RequireSecObjectIndicates that the method calls another method containing security code. Reserved flag for runtime use only.
ReservedMaskIndicates a reserved flag for runtime use only.
ReuseSlotIndicates that the method will reuse an existing slot in the vtable. This is the default behavior.
RTSpecialNameIndicates that the common language runtime checks the name encoding.
SpecialNameIndicates that the method is special. The name describes how this method is special.
StaticIndicates that the method is defined on the type; otherwise, it is defined per instance.
UnmanagedExportIndicates that the managed method is exported by thunk to unmanaged code.
VirtualIndicates that the method is virtual.
VtableLayoutMaskRetrieves vtable attributes.

The following example displays some of the attributes of an example method, and an attribute of a property accessor.


using System;
using System.Reflection;

class Example
{
   protected virtual internal void MyMethod(int a, ref string b)
   {
      b = "in MyMethod";
   }

   public int P { get { return 42; }}

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Reflection.MethodBase.Attributes Sample\n\n";

      // Get the method and its attributes.
      MethodBase mb = typeof(Example).GetMethod("MyMethod",
                              BindingFlags.NonPublic | BindingFlags.Instance);
      MethodAttributes attribs = mb.Attributes;

      // Display the method name and signature.
      outputBlock.Text += "MethodBase.ToString(): " + mb.ToString() + "\n";

      // Method access is a number, not a flag.
      string memberAccess = "";
      switch (attribs & MethodAttributes.MemberAccessMask)
      {
         case MethodAttributes.PrivateScope:
            memberAccess = "PrivateScope - member is not referenceable";
            break;
         case MethodAttributes.Private:
            memberAccess = "Private";
            break;
         case MethodAttributes.FamANDAssem:
            memberAccess = "FamANDAssem - derived types that are also restricted to the assembly\n" +
               "               This access level cannot be created with C#.\n";
            break;
         case MethodAttributes.Assembly:
            memberAccess = "Assembly - internal";
            break;
         case MethodAttributes.Family:
            memberAccess = "Family - protected";
            break;
         case MethodAttributes.FamORAssem:
            memberAccess = "FamORAssem - protected internal";
            break;
         case MethodAttributes.Public:
            memberAccess = "Public";
            break;
      }

      outputBlock.Text += "Access level: " + memberAccess + "\n";

      MethodAttributes vtable = attribs & MethodAttributes.VtableLayoutMask;
      if (vtable == MethodAttributes.ReuseSlot)
      {
         outputBlock.Text += "Method will reuse an existing slot in the vtable.\n" ;
      }
      else
      {
         outputBlock.Text += "Method always gets a new slot in the vtable.\n";
      }

      if ((attribs & MethodAttributes.Virtual) != 0)
      {
         outputBlock.Text += "Method is overridable.\n" ;
      }
      else
      {
         outputBlock.Text += "Method cannot be overridden.\n";
      }

      MethodInfo propertyGetter = 
         typeof(Example).GetProperty("P").GetGetMethod();
      if (0 != (propertyGetter.Attributes & MethodAttributes.SpecialName))
      {
         outputBlock.Text +=
            String.Format("\nProperty accessor '{0}' has a special name.\n", 
            propertyGetter);
      }
   }
}

/* This code produces output similar to the following:

Reflection.MethodBase.Attributes Sample

MethodBase.ToString(): Void MyMethod(Int32, System.String ByRef, System.String.ByRef)
Access level: FamORAssem - protected internal
Method will reuse an existing slot in the vtable.

Property accessor 'Int32 get_P()' has a special name.
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.