C# MethodInfo Attributes

Description

MethodInfo Attributes Gets the attributes associated with this method.

Syntax

MethodInfo.Attributes has the following syntax.


public abstract MethodAttributes Attributes { get; }

Example

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


//  w  ww . ja  v a  2s . c  o  m
using System;
using System.Reflection;

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

    public static int Main(string[] args)
    {      
        Type MyType = Type.GetType("AttributesSample");
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");
        Console.WriteLine(Mymethodbase);
        MethodAttributes Myattributes = Mymethodbase.Attributes;
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }
    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        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)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo