MethodAttributes Enumeração

Definição

Especifica flags para atributos de método. Estas bandeiras estão definidas no ficheiro corhdr.h.

Esta enumeração suporta uma combinação bit-a-bit dos respetivos valores membro.

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
Herança
MethodAttributes
Atributos

Campos

Name Valor Description
PrivateScope 0

Indica que o membro não pode ser referenciado.

ReuseSlot 0

Indica que o método irá reutilizar um slot existente no vtable. Este é o comportamento padrão.

Private 1

Indica que o método é acessível apenas à classe atual.

FamANDAssem 2

Indica que o método é acessível a membros deste tipo e dos seus tipos derivados que estão apenas neste conjunto.

Assembly 3

Indica que o método é acessível a qualquer classe deste conjunto.

Family 4

Indica que o método é acessível apenas aos membros desta classe e às suas classes derivadas.

FamORAssem 5

Indica que o método é acessível a classes derivadas em qualquer lugar, bem como a qualquer classe na assembleia.

Public 6

Indica que o método é acessível a qualquer objeto para o qual este objeto esteja no âmbito.

MemberAccessMask 7

Recupera informações de acessibilidade.

UnmanagedExport 8

Indica que o método gerido é exportado por thunk para código não gerido.

Static 16

Indica que o método está definido no tipo; caso contrário, é definido por instância.

Final 32

Indica que o método não pode ser anulado.

Virtual 64

Indica que o método é virtual.

HideBySig 128

Indica que o método se esconde por nome e assinatura; caso contrário, apenas pelo nome.

NewSlot 256

Indica que o método recebe sempre um novo slot no vtable.

VtableLayoutMask 256

Recupera atributos vtable.

CheckAccessOnOverride 512

Indica que o método só pode ser anulado quando também está acessível.

Abstract 1024

Indica que a classe não fornece uma implementação deste método.

SpecialName 2048

Indica que o método é especial. O nome descreve como este método é especial.

RTSpecialName 4096

Indica que o runtime da linguagem comum verifica a codificação do nome.

PinvokeImpl 8192

Indica que a implementação do método é encaminhada através do PInvoke (Platform Invocation Services).

HasSecurity 16384

Indica que o método tem segurança associada. Flag reservado apenas para uso em tempo de execução.

RequireSecObject 32768

Indica que o método chama outro método contendo código de segurança. Flag reservado apenas para uso em tempo de execução.

ReservedMask 53248

Indica uma flag reservada apenas para uso em tempo de execução.

Exemplos

O exemplo seguinte mostra os atributos do método especificado.

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

Aplica-se a