MethodAttributes 列舉

定義

指定方法屬性的旗標。 這些旗標定義在 corhdr.h 檔案中。

此列舉支援其成員值的位元組合。

public enum class MethodAttributes
[System.Flags]
public enum MethodAttributes
[System.Flags]
[System.Serializable]
public enum MethodAttributes
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum MethodAttributes
[<System.Flags>]
type MethodAttributes = 
[<System.Flags>]
[<System.Serializable>]
type MethodAttributes = 
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MethodAttributes = 
Public Enum MethodAttributes
繼承
MethodAttributes
屬性

欄位

名稱 Description
PrivateScope 0

表示該成員無法被引用。

ReuseSlot 0

表示該方法會重複使用vtable中的現有槽位。 此為預設行為。

Private 1

表示該方法僅對目前類別可存取。

FamANDAssem 2

表示該方法僅對此類型及其衍生型別的成員(且僅屬於此組件)可存取。

Assembly 3

表示該方法對該組合的任一類別皆可存取。

Family 4

表示該方法僅對此類別及其衍生類別的成員可存取。

FamORAssem 5

表示該方法可被任何衍生類別存取,也能存取組合語言中的任何類別。

Public 6

表示該方法對任何該物件在作用範圍內的物件皆可存取。

MemberAccessMask 7

取得無障礙資訊。

UnmanagedExport 8

表示受管理方法會由 Thunk 匯出成非受管理程式碼。

Static 16

表示該方法定義在型別上;否則,則是依實例定義。

Final 32

表示該方法無法被覆寫。

Virtual 64

表示該方法是虛擬的。

HideBySig 128

表示該方法可透過名稱與簽名隱藏;否則,僅以姓名命名。

NewSlot 256

表示該方法總是會在 vtable 中獲得新的插槽。

VtableLayoutMask 256

取回 vtable 屬性。

CheckAccessOnOverride 512

表示該方法只有在可存取時才能被覆寫。

Abstract 1024

表示該類別未提供此方法的實作。

SpecialName 2048

表示這個方法很特別。 這個名稱說明了這種方法的特殊之處。

RTSpecialName 4096

表示共通語言執行時會檢查名稱編碼。

PinvokeImpl 8192

表示方法實作透過 PInvoke(平台呼叫服務)轉發。

HasSecurity 16384

表示該方法具有安全性。 保留旗標僅供執行時使用。

RequireSecObject 32768

表示該方法呼叫另一個包含安全碼的方法。 保留旗標僅供執行時使用。

ReservedMask 53248

表示僅用於執行時的保留旗標。

範例

以下範例展示指定方法的屬性。

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 = (int)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}
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.")
            Return
        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
                Console.WriteLine(fields(i).Name)
            End If
        Next i
    End Sub
End Class

適用於