閱讀英文版本

分享方式:


MethodAttributes 列舉

定義

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

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

C#
[System.Flags]
public enum MethodAttributes
C#
[System.Flags]
[System.Serializable]
public enum MethodAttributes
C#
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum MethodAttributes
繼承
MethodAttributes
屬性

欄位

名稱 Description
Abstract 1024

指示類別不提供這個方法的實作。

Assembly 3

指示這個組件的任何類別可存取該方法。

CheckAccessOnOverride 512

指示方法只有在它也是可存取時才可以被覆寫。

FamANDAssem 2

指示該方法只能讓這個型別的成員,以及這個型別在這個組件中的衍生型別存取。

Family 4

指示只有這個類別和其衍生類別的成員可以存取該方法。

FamORAssem 5

指示任何位置的衍生類別以及組件中的任何類別都可存取該方法。

Final 32

指示這個方法不能被覆寫。

HasSecurity 16384

指示方法具有與它相關的安全性。 保留旗標僅供執行階段使用。

HideBySig 128

指示方法依名稱和簽章隱藏;否則只依名稱隱藏。

MemberAccessMask 7

擷取存取範圍資訊。

NewSlot 256

指示方法永遠取得 vtable 中的新位置。

PinvokeImpl 8192

指示方法實作經由 PInvoke (平台引動服務) 轉寄。

Private 1

指示這個方法只能讓目前的類別存取。

PrivateScope 0

指示成員不能被參考。

Public 6

指示這個物件所在範圍內的任何物件可存取該方法。

RequireSecObject 32768

指示方法呼叫含有安全程式碼的另一個方法。 保留旗標僅供執行階段使用。

ReservedMask 53248

指示保留旗標僅供執行階段使用。

ReuseSlot 0

指示方法將重新使用 vtable 中的現有位置。 此為預設行為。

RTSpecialName 4096

指示 Common Language Runtime 檢查名稱編碼方式。

SpecialName 2048

指示方法為特殊方法。 該名稱描述這個方法是如何特殊。

Static 16

指示方法是定義在型別上;否則就是定義在每個執行個體 (Instance) 上。

UnmanagedExport 8

指示 Managed 方法由 Thunk 匯出到 Unmanaged 程式碼。

Virtual 64

指示方法為虛擬的。

VtableLayoutMask 256

擷取 vtable 屬性。

範例

下列範例會顯示指定方法的屬性。

C#
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);
            }
        }
    }
}

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0