Mono Class Library: System.Reflection.MethodBase Overview | Members

System.Reflection.MethodBase.Attributes Property

Gets the attributes of the method reflected by the current instance. [Edit]

public abstract MethodAttributes Attributes { get; }

Value

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

Remarks

Operation

This property is read-only.

This property gets a System.Reflection.MethodAttributes value that indicates the attributes set in the metadata of the method reflected by the current instance.

Usage
Use this property to determine the accessibility, layout, and semantics of the constructor or method reflected by the current instance. Also use this property to determine if the member reflected by the current instance is implemented in native code or has a special name.

[Edit]

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