MethodImplAttribute Classe

Définition

Spécifie les détails de l'implémentation d'une méthode. Cette classe ne peut pas être héritée.

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
Héritage
MethodImplAttribute
Attributs

Exemples

L’exemple suivant applique la MethodImplAttribute GetCalendarName méthode pour s’assurer qu’elle n’est pas inline au moment de l’exécution par le compilateur juste-à-temps (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

L’exemple suivant appelle ensuite la MethodInfo.GetMethodImplementationFlags méthode pour déterminer quels indicateurs sont définis pour la GetCalendarName méthode. Elle montre également que ces informations ne sont pas récupérées par la MemberInfo.GetCustomAttributes méthode.

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

Remarques

Vous pouvez appliquer cet attribut aux méthodes ou constructeurs.

Cet attribut vous permet de personnaliser la configuration de la méthode ou du constructeur auquel elle s’applique en fournissant une MethodImplOptions valeur à son constructeur de classe. Les membres de l’énumération correspondent aux champs de bits de la MethodImplOptions CorMethodImpl table de métadonnées. Cela signifie que les informations sur l’attribut ne peuvent pas être récupérées au moment de l’exécution en appelant la méthode ; à la MemberInfo.GetCustomAttributes place, elles sont récupérées en appelant la MethodInfo.GetMethodImplementationFlags méthode ou la ConstructorInfo.GetMethodImplementationFlags méthode.

Constructeurs

MethodImplAttribute()

Initialise une nouvelle instance de la classe MethodImplAttribute.

MethodImplAttribute(Int16)

Initialise une nouvelle instance de la classe MethodImplAttribute avec la valeur MethodImplOptions spécifiée.

MethodImplAttribute(MethodImplOptions)

Initialise une nouvelle instance de la classe MethodImplAttribute avec la valeur MethodImplOptions spécifiée.

Champs

MethodCodeType

Valeur MethodCodeType indiquant le type d'implémentation fourni pour cette méthode.

Propriétés

TypeId

Lors de l'implémentation dans une classe dérivée, obtient un identificateur unique pour l'objet Attribute.

(Hérité de Attribute)
Value

Obtient la valeur MethodImplOptions qui décrit la méthode avec attributs.

Méthodes

Equals(Object)

Retourne une valeur qui indique si cette instance est égale à un objet spécifié.

(Hérité de Attribute)
GetHashCode()

Retourne le code de hachage de cette instance.

(Hérité de Attribute)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
IsDefaultAttribute()

En cas de substitution dans une classe dérivée, indique si la valeur de cette instance est la valeur par défaut pour la classe dérivée.

(Hérité de Attribute)
Match(Object)

En cas de substitution dans une classe dérivée, retourne une valeur indiquant si cette instance équivaut à un objet spécifié.

(Hérité de Attribute)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

Implémentations d’interfaces explicites

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

Mappe un jeu de noms avec un jeu correspondant d'identificateurs de dispatch.

(Hérité de Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Récupère les informations de type pour un objet, qui peuvent être utilisées pour obtenir les informations de type d'une interface.

(Hérité de Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Récupère le nombre d'interfaces d'informations de type fourni par un objet (0 ou 1).

(Hérité de Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fournit l'accès aux propriétés et aux méthodes exposées par un objet.

(Hérité de Attribute)

S’applique à

Voir aussi