MethodImplAttribute Clase

Definición

Especifica los detalles de cómo se implementa un método. Esta clase no puede heredarse.

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
Herencia
MethodImplAttribute
Atributos

Ejemplos

En el ejemplo siguiente se aplica al MethodImplAttribute GetCalendarName método para asegurarse de que no está insertado en tiempo de ejecución por el 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

A continuación, en el ejemplo siguiente se llama al MethodInfo.GetMethodImplementationFlags método para determinar qué marcas se establecen para el GetCalendarName método . También muestra que el MemberInfo.GetCustomAttributes método no recupera esta información.

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

Comentarios

Puede aplicar este atributo a métodos o constructores.

Este atributo permite personalizar la configuración del método o constructor al que se aplica proporcionando un MethodImplOptions valor a su constructor de clase. Los miembros de la MethodImplOptions enumeración corresponden a campos de bits de la CorMethodImpl tabla de metadatos. Esto significa que la información sobre el atributo no se puede recuperar en tiempo de ejecución llamando al MemberInfo.GetCustomAttributes método ; en su lugar, se recupera llamando al MethodInfo.GetMethodImplementationFlags método o ConstructorInfo.GetMethodImplementationFlags .

Constructores

MethodImplAttribute()

Inicializa una nueva instancia de la clase MethodImplAttribute.

MethodImplAttribute(Int16)

Inicializa una nueva instancia de la clase MethodImplAttribute con el valor MethodImplOptions especificado.

MethodImplAttribute(MethodImplOptions)

Inicializa una nueva instancia de la clase MethodImplAttribute con el valor MethodImplOptions especificado.

Campos

MethodCodeType

Valor de MethodCodeType que indica el tipo de implementación proporcionado para este método.

Propiedades

TypeId

Cuando se implementa en una clase derivada, obtiene un identificador único para este Attribute.

(Heredado de Attribute)
Value

Obtiene el valor de MethodImplOptions que describe el método al que se aplicó el atributo.

Métodos

Equals(Object)

Devuelve un valor que indica si esta instancia es igual que un objeto especificado.

(Heredado de Attribute)
GetHashCode()

Devuelve el código hash de esta instancia.

(Heredado de Attribute)
GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
IsDefaultAttribute()

Si se reemplaza en una clase derivada, indica si el valor de esta instancia es el valor predeterminado de la clase derivada.

(Heredado de Attribute)
Match(Object)

Cuando se invalida en una clase derivada, devuelve un valor que indica si esta instancia es igual a un objeto especificado.

(Heredado de Attribute)
MemberwiseClone()

Crea una copia superficial del Object actual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Implementaciones de interfaz explícitas

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Asigna un conjunto de nombres a un conjunto correspondiente de identificadores de envío.

(Heredado de Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Obtiene la información de tipos de un objeto, que puede utilizarse para obtener la información de tipos de una interfaz.

(Heredado de Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera el número de interfaces de información de tipo que proporciona un objeto (0 ó 1).

(Heredado de Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Proporciona acceso a las propiedades y los métodos expuestos por un objeto.

(Heredado de Attribute)

Se aplica a

Consulte también