MethodBase.Attributes Property
Gets the attributes associated with this method.
Assembly: mscorlib (in mscorlib.dll)
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.
Notes to ImplementersUse the Attributes property to determine whether a method is public, private, final, virtual, and so on.
The following code example displays the attributes of the user-defined method Mymethod.
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) { Console.WriteLine ("Reflection.MethodBase.Attributes Sample"); // Get the type. Type MyType = Type.GetType("AttributesSample"); // Get the method Mymethod on the type. MethodBase Mymethodbase = MyType.GetMethod("Mymethod"); // Display the method name. Console.WriteLine("Mymethodbase = " + Mymethodbase); // Get the MethodAttribute enumerated value. MethodAttributes Myattributes = Mymethodbase.Attributes; // Display the flags that are set. PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes); return 0; } public static void PrintAttributes(Type attribType, int iAttribValue) { if (!attribType.IsEnum) { Console.WriteLine("This type is not an enum."); 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) { Console.WriteLine(fields[i].Name); } } } }
This code produces the following output:
Reflection.MethodBase.Attributes Sample
Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef)
PrivateScope
FamANDAssem
Family
Public
HideBySig
ReuseSlot
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.