MethodAttributes 列舉


指定方法屬性 (Attribute) 的旗標。Specifies flags for method attributes. 這些旗標都被定義在 corhdr.h 檔案中。These flags are defined in the corhdr.h file.

此列舉具有 FlagsAttribute 個屬性允許以位元方式合併其成員值。

public enum class MethodAttributes
public enum MethodAttributes
type MethodAttributes = 
Public Enum MethodAttributes


Abstract 1024

指示類別不提供這個方法的實作。Indicates that the class does not provide an implementation of this method.

Assembly 3

指示這個組件的任何類別可存取該方法。Indicates that the method is accessible to any class of this assembly.

CheckAccessOnOverride 512

指示方法只有在它也是可存取時才可以被覆寫。Indicates that the method can only be overridden when it is also accessible.

FamANDAssem 2

指示該方法只能讓這個型別的成員,以及這個型別在這個組件中的衍生型別存取。Indicates that the method is accessible to members of this type and its derived types that are in this assembly only.

Family 4

指示只有這個類別和其衍生類別的成員可以存取該方法。Indicates that the method is accessible only to members of this class and its derived classes.

FamORAssem 5

指示任何位置的衍生類別以及組件中的任何類別都可存取該方法。Indicates that the method is accessible to derived classes anywhere, as well as to any class in the assembly.

Final 32

指示這個方法不能被覆寫。Indicates that the method cannot be overridden.

HasSecurity 16384

指示方法具有與它相關的安全性。Indicates that the method has security associated with it. 保留旗標僅供執行階段使用。Reserved flag for runtime use only.

HideBySig 128

指示方法依名稱和簽章隱藏;否則只依名稱隱藏。Indicates that the method hides by name and signature; otherwise, by name only.

MemberAccessMask 7

擷取存取範圍資訊。Retrieves accessibility information.

NewSlot 256

指示方法永遠取得 vtable 中的新位置。Indicates that the method always gets a new slot in the vtable.

PinvokeImpl 8192

指示方法實作經由 PInvoke (平台引動服務) 轉寄。Indicates that the method implementation is forwarded through PInvoke (Platform Invocation Services).

Private 1

指示這個方法只能讓目前的類別存取。Indicates that the method is accessible only to the current class.

PrivateScope 0

指示成員不能被參考。Indicates that the member cannot be referenced.

Public 6

指示這個物件所在範圍內的任何物件可存取該方法。Indicates that the method is accessible to any object for which this object is in scope.

RequireSecObject 32768

指示方法呼叫含有安全程式碼的另一個方法。Indicates that the method calls another method containing security code. 保留旗標僅供執行階段使用。Reserved flag for runtime use only.

ReservedMask 53248

指示保留旗標僅供執行階段使用。Indicates a reserved flag for runtime use only.

ReuseSlot 0

指示方法將重新使用 vtable 中的現有位置。Indicates that the method will reuse an existing slot in the vtable. 這是預設行為。This is the default behavior.

RTSpecialName 4096

指示 Common Language Runtime 檢查名稱編碼方式。Indicates that the common language runtime checks the name encoding.

SpecialName 2048

指示方法為特殊方法。Indicates that the method is special. 該名稱描述這個方法是如何特殊。The name describes how this method is special.

Static 16

指示方法是定義在型別上;否則就是定義在每個執行個體 (Instance) 上。Indicates that the method is defined on the type; otherwise, it is defined per instance.

UnmanagedExport 8

指示 Managed 方法由 Thunk 匯出到 Unmanaged 程式碼。Indicates that the managed method is exported by thunk to unmanaged code.

Virtual 64

指示方法為虛擬的。Indicates that the method is virtual.

VtableLayoutMask 256

擷取 vtable 屬性。Retrieves vtable attributes.


下列範例會顯示指定方法的屬性。The following example displays the attributes of the specified method.

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

public ref class AttributesSample
   void Mymethod( int int1m, [Out]interior_ptr<String^> str2m, interior_ptr<String^> str3m )
       *str2m = "in Mymethod";

void PrintAttributes( Type^ attribType, int iAttribValue )
   if (  !attribType->IsEnum )
      Console::WriteLine( "This type is not an enum." );

   array<FieldInfo^>^fields = attribType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static) );
   for ( int i = 0; i < fields->Length; i++ )
      int fieldvalue = safe_cast<Int32>(fields[ i ]->GetValue( nullptr ));
      if ( (fieldvalue & iAttribValue) == fieldvalue )
         Console::WriteLine( fields[ i ]->Name );

int main()
   Console::WriteLine( "Reflection.MethodBase.Attributes Sample" );

   // Get the type of the chosen class.
   Type^ MyType = Type::GetType( "AttributesSample" );

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

   // Display the method name and signature.
   Console::WriteLine( "Mymethodbase = {0}", Mymethodbase );

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

   // Display the flags that are set.
   PrintAttributes( System::Reflection::MethodAttributes::typeid, (int)Myattributes );
   return 0;
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 of the chosen class.
        Type MyType = Type.GetType("AttributesSample");
        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");
        // Display the method name and signature.
        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)
Imports System.Reflection

Class AttributesSample

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

    Public Shared Function Main(ByVal args() As String) As Integer
        Console.WriteLine("Reflection.MethodBase.Attributes Sample")

        ' Get the type of a chosen class.
        Dim MyType As Type = Type.GetType("AttributesSample")

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

        ' Display the method name and signature.
        Console.WriteLine("Mymethodbase = {0}", Mymethodbase)

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

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

    Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer)
        If Not attribType.IsEnum Then
            Console.WriteLine("This type is not an enum.")
        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
            End If
        Next i
    End Sub
End Class