System.Reflection.MethodBase.Attributes Property

Gets the attributes associated with this method.

Syntax

public abstract MethodAttributes Attributes { get; }

Value

A System.Reflection.MethodAttributes value that signifies the attributes of the method reflected by the current instance.

Remarks

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

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

Example

The following example demonstrates using this property to obtain the attributes of three methods.

C# Example

using System;
using System.Reflection;

abstract class MyBaseClass
{

   abstract public void MyPublicInstanceMethod();

}

class MyDerivedClass : MyBaseClass
{

   public override void MyPublicInstanceMethod() {}
   private static void MyPrivateStaticMethod() {}

}

class MethodAttributesExample
{

   static void PrintMethodAttributes(Type t)
   {

      string str;
      MethodInfo[] miAry = t.GetMethods( BindingFlags.Static |
         BindingFlags.Instance | BindingFlags.Public |
         BindingFlags.NonPublic | BindingFlags.DeclaredOnly );
      foreach (MethodInfo mi in miAry)
      {

         Console.WriteLine("Method {0} is: ", mi.Name);
         str = ((mi.Attributes & MethodAttributes.Static) != 0) ?
            "Static" : "Instance";
         Console.Write(str + " ");
         str = ((mi.Attributes & MethodAttributes.Public) != 0) ?
            "Public" : "Not-Public";
         Console.Write(str + " ");
         str = ((mi.Attributes & MethodAttributes.HideBySig) != 0) ?
            "HideBySig" : "Hide-by-name";
         Console.Write(str + " ");
         str = ((mi.Attributes & MethodAttributes.Abstract) != 0) ?
            "Abstract" : String.Empty;
         Console.WriteLine(str);

      }

   }

   public static void Main()
   {

      PrintMethodAttributes(typeof(MyBaseClass));
      PrintMethodAttributes(typeof(MyDerivedClass));

   }

}
      

The output is

Method MyPublicInstanceMethod is:
Instance Public HideBySig Abstract
Method MyPublicInstanceMethod is:
Instance Public HideBySig
Method MyPrivateStaticMethod is:
Static Not-Public HideBySig

Requirements

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0