MethodAttributes Enumeration

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<FlagsAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration MethodAttributes
[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum MethodAttributes

Members

Member name Description
Supported by Silverlight for Windows PhoneSupported by Xbox 360 MemberAccessMask Retrieves accessibility information.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 PrivateScope Indicates that the member cannot be referenced.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Private Indicates that the method is accessible only to the current class.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 FamANDAssem Indicates that the method is accessible to members of this type and its derived types that are in this assembly only.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Assembly Indicates that the method is accessible to any class of this assembly.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Family Indicates that the method is accessible only to members of this class and its derived classes.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 FamORAssem Indicates that the method is accessible to derived classes anywhere, as well as to any class in the assembly.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Public Indicates that the method is accessible to any object for which this object is in scope.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Static Indicates that the method is defined on the type; otherwise, it is defined per instance.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Final Indicates that the method cannot be overridden.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Virtual Indicates that the method is virtual.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 HideBySig Indicates that the method hides by name and signature; otherwise, by name only.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 CheckAccessOnOverride Indicates that the method can only be overridden when it is also accessible.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 VtableLayoutMask Retrieves vtable attributes.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 ReuseSlot Indicates that the method will reuse an existing slot in the vtable. This is the default behavior.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 NewSlot Indicates that the method always gets a new slot in the vtable.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Abstract Indicates that the class does not provide an implementation of this method.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 SpecialName Indicates that the method is special. The name describes how this method is special.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 PinvokeImpl Indicates that the method implementation is forwarded through PInvoke (Platform Invocation Services).
Supported by Silverlight for Windows PhoneSupported by Xbox 360 UnmanagedExport Indicates that the managed method is exported by thunk to unmanaged code.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 RTSpecialName Indicates that the common language runtime checks the name encoding.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 ReservedMask Indicates a reserved flag for runtime use only.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 HasSecurity Indicates that the method has security associated with it. Reserved flag for runtime use only.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 RequireSecObject Indicates that the method calls another method containing security code. Reserved flag for runtime use only.

Examples

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

Imports System.Reflection

Class Example

   Protected Overridable Friend Sub MyMethod(ByVal a As Integer, ByRef b As String)
      b = "in MyMethod"
   End Sub 

   Public ReadOnly Property P() As Integer
      Get
         Return 42
      End Get
   End Property

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      outputBlock.Text &= "Reflection.MethodBase.Attributes Sample" & vbLf & vbLf

      ' Get the method and its attributes.
      Dim mb As MethodBase = GetType(Example).GetMethod("MyMethod", _
                                 BindingFlags.NonPublic Or BindingFlags.Instance)
      Dim attribs As MethodAttributes = mb.Attributes

      ' Display the method name and signature.
      outputBlock.Text &= "MethodBase.ToString(): " & mb.ToString() & vbLf

      ' Method access is a number, not a flag.
      Dim memberAccess As String = ""
      Select Case attribs And MethodAttributes.MemberAccessMask
         Case MethodAttributes.PrivateScope:
            memberAccess = "PrivateScope - member is not referenceable"
         Case MethodAttributes.Private:
            memberAccess = "Private"
         Case MethodAttributes.FamANDAssem:
            memberAccess = "FamANDAssem - derived types that are also restricted to the assembly" & _
               vbLf & "               This access level cannot be created with Visual Basic." & vbLf         
         Case MethodAttributes.Assembly:
            memberAccess = "Assembly - Friend"
         Case MethodAttributes.Family:
            memberAccess = "Family - Protected"
         Case MethodAttributes.FamORAssem:
            memberAccess = "FamORAssem - Protected Friend"
         Case MethodAttributes.Public:
            memberAccess = "Public"
      End Select

      outputBlock.Text &= "Access level: " & memberAccess & vbLf

      Dim vtable As MethodAttributes = attribs And MethodAttributes.VtableLayoutMask
      If vtable = MethodAttributes.ReuseSlot
         outputBlock.Text &= "Method will reuse an existing slot in the vtable." & vbLf
      Else
         outputBlock.Text &= "Method always gets a new slot in the vtable." & vbLf
      End If

      If (attribs And MethodAttributes.Virtual) <> 0 Then
         outputBlock.Text &= "Method is overridable." & vbLf
      Else
         outputBlock.Text &= "Method cannot be overridden." & vbLf
      End If

      Dim propertyGetter As MethodInfo = _
         GetType(Example).GetProperty("P").GetGetMethod()
      If 0 <> (propertyGetter.Attributes And MethodAttributes.SpecialName) Then
         outputBlock.Text &= vbLf & _
            String.Format("Property accessor '{0}' has a special name." & vbLf, _
            propertyGetter)
      End If
   End Sub 
End Class 

' 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 Friend
'Method will reuse an existing slot in the vtable.
'Method is overridable.
'
'Property accessor 'Int32 get_P()' has a special name.
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.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference