MethodImplAttribute Classe
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Especifica os detalhes de como um método é implementado. Esta classe não pode ser herdada.
public ref class MethodImplAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
public sealed class MethodImplAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
[System.Serializable]
public sealed class MethodImplAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class MethodImplAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)>]
type MethodImplAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)>]
[<System.Serializable>]
type MethodImplAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MethodImplAttribute = class
inherit Attribute
Public NotInheritable Class MethodImplAttribute
Inherits Attribute
- Herança
- Atributos
Exemplos
O exemplo seguinte aplica-se ao MethodImplAttributeGetCalendarName método para garantir que não é inlineado em tempo de execução pelo compilador just-in-time (JIT).
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
public class Utility
{
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static string GetCalendarName(Calendar cal)
{
return cal.ToString().Replace("System.Globalization.", "").
Replace("Calendar", "");
}
}
Imports System.Globalization
Imports System.Runtime.CompilerServices
Public Class Utility
<MethodImplAttribute(MethodImplOptions.NoInlining)>
Public Shared Function GetCalendarName(cal As Calendar) As String
Return cal.ToString().Replace("System.Globalization.", "").Replace("Calendar", "")
End Function
End Class
O exemplo seguinte chama então o MethodInfo.GetMethodImplementationFlags método para determinar quais as bandeiras definidas para o GetCalendarName método. Também demonstra que esta informação não é recuperada pelo MemberInfo.GetCustomAttributes método.
using System;
using System.Reflection;
public class Example
{
public static void Main()
{
// Use reflection to get a reference to the GetCalendarName method.
Assembly assem = Assembly.LoadFrom(@".\Example.dll");
Type type = assem.GetType("Utility");
MethodInfo methodInfo = type.GetMethod("GetCalendarName");
// Determine whether the method has any custom attributes.
Console.Write("Utility.GetCalendarName custom attributes:");
object[] attribs = methodInfo.GetCustomAttributes(false);
if (attribs.Length > 0) {
Console.WriteLine();
foreach (var attrib in attribs)
Console.WriteLine(" " + attrib.ToString());
}
else {
Console.WriteLine(" <None>");
}
// Get the method's metadata flags.
MethodImplAttributes flags = methodInfo.GetMethodImplementationFlags();
Console.WriteLine("Utility.GetCalendarName flags: {0}",
flags.ToString());
}
}
// The example displays the following output:
// Utility.GetCalendarName custom attributes: <None>
// Utility.GetCalendarName flags: NoInlining
Imports System.Reflection
Module Example
Public Sub Main()
' Use reflection to get a reference to the GetCalendarName method.
Dim assem As Assembly = Assembly.LoadFrom(".\Example.dll")
Dim type As Type = assem.GetType("Utility")
Dim methodInfo As MethodInfo = type.GetMethod("GetCalendarName")
' Determine whether the method has any custom attributes.
Console.Write("Utility.GetCalendarName custom attributes:")
Dim attribs() As Object = methodInfo.GetCustomAttributes(False)
If attribs.Length > 0 Then
Console.WriteLine()
For Each attrib As Object In attribs
Console.WriteLine(" " + attrib.ToString())
Next
Else
Console.WriteLine(" <None>")
End If
' Get the method's metadata flags.
Dim flags As MethodImplAttributes = methodInfo.GetMethodImplementationFlags()
Console.WriteLine("Utility.GetCalendarName flags: {0}", flags.ToString())
End Sub
End Module
' The example displays the following output:
' Utility.GetCalendarName custom attributes: <None>
' Utility.GetCalendarName flags: NoInlining
Observações
Pode aplicar esta característica a métodos ou construtores.
Este atributo permite-lhe personalizar a configuração do método ou construtor a que se aplica, fornecendo um MethodImplOptions valor ao seu construtor de classe. Os membros da MethodImplOptions enumeração correspondem a campos de bits na CorMethodImpl tabela de metadados. Isto significa que a informação sobre o atributo não pode ser recuperada em tempo de execução chamando o MemberInfo.GetCustomAttributes método; em vez disso, é recuperada chamando ou o MethodInfo.GetMethodImplementationFlags ou o ConstructorInfo.GetMethodImplementationFlags método.
Construtores
| Name | Description |
|---|---|
| MethodImplAttribute() |
Inicializa uma nova instância da MethodImplAttribute classe. |
| MethodImplAttribute(Int16) |
Inicializa uma nova instância da MethodImplAttribute classe com o valor especificado MethodImplOptions . |
| MethodImplAttribute(MethodImplOptions) |
Inicializa uma nova instância da MethodImplAttribute classe com o valor especificado MethodImplOptions . |
Campos
| Name | Description |
|---|---|
| MethodCodeType |
Um valor que MethodCodeType indica que tipo de implementação é fornecida para este método. |
Propriedades
| Name | Description |
|---|---|
| TypeId |
Quando implementado numa classe derivada, obtém um identificador único para esta Attribute. (Herdado de Attribute) |
| Value |
Obtém o MethodImplOptions valor que descreve o método atributo. |
Métodos
| Name | Description |
|---|---|
| Equals(Object) |
Devolve um valor que indica se esta instância é igual a um objeto especificado. (Herdado de Attribute) |
| GetHashCode() |
Devolve o código de hash para esta instância. (Herdado de Attribute) |
| GetType() |
Obtém o Type da instância atual. (Herdado de Object) |
| IsDefaultAttribute() |
Quando sobrescrito numa classe derivada, indica se o valor desta instância é o valor padrão para a classe derivada. (Herdado de Attribute) |
| Match(Object) |
Quando sobrescrito numa classe derivada, devolve um valor que indica se esta instância é igual a um objeto especificado. (Herdado de Attribute) |
| MemberwiseClone() |
Cria uma cópia superficial do atual Object. (Herdado de Object) |
| ToString() |
Devolve uma cadeia que representa o objeto atual. (Herdado de Object) |
Implementações de Interface Explícita
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Mapeia um conjunto de nomes para um conjunto correspondente de identificadores de despacho. (Herdado de Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Recupera a informação de tipo de um objeto, que pode ser usada para obter a informação de tipo para uma interface. (Herdado de Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Recupera o número de interfaces de informações de tipo que um objeto fornece (0 ou 1). (Herdado de Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Proporciona acesso a propriedades e métodos expostos por um objeto. (Herdado de Attribute) |