MethodBase.Attributes Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets the attributes associated with this method.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

Syntax

Public MustOverride ReadOnly Property Attributes As MethodAttributes
public abstract MethodAttributes Attributes { get; }

Property Value

Type: System.Reflection..::.MethodAttributes
The attributes of the method.

Remarks

All members have a set of attributes, which are defined in relation to the specific type of member.

To get the MethodAttributes, first get the type. From the type, get the method. From the method, get the MethodAttributes.

Examples

The following code example displays the attributes of the user-defined method Mymethod.

Imports System.Reflection

Class Example

   Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
      str2m = "in Mymethod"
   End Sub 

   Public Shared Function Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) As Integer
      outputBlock.Text &= "Reflection.MethodBase.Attributes Sample" & vbCrLf

      ' Get the type.
      Dim MyType As Type = GetType(Example)

      ' Get the method Mymethod on the type.
      Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod")

      ' Display the method name.
      outputBlock.Text += String.Format("Mymethodbase = {0}.", Mymethodbase) & vbCrLf

      ' Get the MethodAttribute enumerated value.
      Dim Myattributes As MethodAttributes = Mymethodbase.Attributes

      ' Display the flags that are set.
      PrintAttributes(outputBlock, GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
      Return 0
   End Function 

   Public Shared Sub PrintAttributes(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal attribType As Type, ByVal iAttribValue As Integer)
      If Not attribType.IsEnum Then
         outputBlock.Text &= "This type is not an enum." & vbCrLf
         Return
      End If
      Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static))
      Dim i As Integer
      For i = 0 To fields.Length - 1
         Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32)
         If (fieldvalue And iAttribValue) = fieldvalue Then
            outputBlock.Text &= fields(i).Name & vbCrLf
         End If
      Next i
   End Sub 
End Class 

' This example produces the following output:
'
'Reflection.MethodBase.Attributes Sample 
'Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef) 
'PrivateScope 
'FamANDAssem 
'Family 
'Public 
'ReuseSlot 

using System;
using System.Reflection;

class Example
{
   public void Mymethod(int int1m, out string str2m, ref string str3m)
   {
      str2m = "in Mymethod";
   }

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

      // Get the type.
      Type MyType = typeof(Example);

      // Get the method Mymethod on the type.
      MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

      // Display the method name.
      outputBlock.Text += "Mymethodbase = " + Mymethodbase + "\n";

      // Get the MethodAttribute enumerated value.
      MethodAttributes Myattributes = Mymethodbase.Attributes;

      // Display the flags that are set.
      PrintAttributes(outputBlock, typeof(System.Reflection.MethodAttributes), (int)Myattributes);
      return 0;
   }


   public static void PrintAttributes(System.Windows.Controls.TextBlock outputBlock, Type attribType, int iAttribValue)
   {
      if (!attribType.IsEnum)
      {
         outputBlock.Text += "This type is not an enum." + "\n";
         return;
      }

      FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
      for (int i = 0; i < fields.Length; i++)
      {
         int fieldvalue = (Int32)fields[i].GetValue(null);
         if ((fieldvalue & iAttribValue) == fieldvalue)
         {
            outputBlock.Text += fields[i].Name + "\n";
         }
      }
   }
}

/* This example produces the following output:

Reflection.MethodBase.Attributes Sample 
Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef) 
PrivateScope 
FamANDAssem 
Family 
Public 
HideBySig 
ReuseSlot 
 */

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

MethodBase Class

System.Reflection Namespace

MethodAttributes