MethodImplAttribute Klasa

Definicja

Określa szczegóły sposobu implementacji metody. Klasa ta nie może być dziedziczona.

C#
[System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
public sealed class MethodImplAttribute : Attribute
C#
[System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
[System.Serializable]
public sealed class MethodImplAttribute : Attribute
C#
[System.AttributeUsage(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class MethodImplAttribute : Attribute
Dziedziczenie
MethodImplAttribute
Atrybuty

Przykłady

Poniższy przykład dotyczy MethodImplAttributeGetCalendarName metody , aby upewnić się, że nie jest on podkreślony w czasie wykonywania przez kompilator just in time (JIT).

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

Poniższy przykład wywołuje następnie metodę, MethodInfo.GetMethodImplementationFlags aby określić, które flagi są ustawione dla GetCalendarName metody. Pokazuje również, że te informacje nie są pobierane przez metodę MemberInfo.GetCustomAttributes .

C#
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

Uwagi

Ten atrybut można zastosować do metod lub konstruktorów.

Ten atrybut umożliwia dostosowanie konfiguracji metody lub konstruktora, do którego ma zastosowanie, dostarczając MethodImplOptions wartość do konstruktora klasy. Elementy członkowskie wyliczenia MethodImplOptions odpowiadają polam bitowym w CorMethodImpl tabeli metadanych. Oznacza to, że nie można pobrać informacji o atrybucie w czasie wykonywania przez wywołanie metody . Zamiast tego jest pobierana przez wywołanie MemberInfo.GetCustomAttributesMethodInfo.GetMethodImplementationFlags metody lub ConstructorInfo.GetMethodImplementationFlags metody .

Konstruktory

MethodImplAttribute()

Inicjuje nowe wystąpienie klasy MethodImplAttribute.

MethodImplAttribute(Int16)

Inicjuje MethodImplAttribute nowe wystąpienie klasy o określonej MethodImplOptions wartości.

MethodImplAttribute(MethodImplOptions)

Inicjuje MethodImplAttribute nowe wystąpienie klasy o określonej MethodImplOptions wartości.

Pola

MethodCodeType

Wartość wskazująca MethodCodeType rodzaj implementacji dla tej metody.

Właściwości

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego elementu Attribute.

(Odziedziczone po Attribute)
Value

MethodImplOptions Pobiera wartość opisującą metodę przypisaną.

Metody

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

(Odziedziczone po Attribute)
GetHashCode()

Zwraca wartość skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
IsDefaultAttribute()

W przypadku zastąpienia w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

W przypadku zastąpienia w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie obiektu, którego można użyć do pobrania informacji o typie interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Produkt Wersje
.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, 10
.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.5, 1.6, 2.0, 2.1
UWP 10.0

Zobacz też