MethodImplAttribute Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje podrobnosti o způsobu implementace metody. Tuto třídu nelze zdědit.
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
- Dědičnost
- Atributy
Příklady
Následující příklad použije u GetCalendarName
metody, MethodImplAttribute aby se zajistilo, že není vložen v době běhu kompilátorem JIT (just-in-time).
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
Následující příklad pak volá metodu MethodInfo.GetMethodImplementationFlags
k určení, které příznaky jsou nastaveny pro metodu GetCalendarName
. Také ukazuje, že tyto informace nejsou načteny metodou MemberInfo.GetCustomAttributes .
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
Poznámky
Tento atribut můžete použít u metod nebo konstruktorů.
Tento atribut umožňuje přizpůsobit konfiguraci metody nebo konstruktoru, na který se vztahuje, zadáním MethodImplOptions hodnoty do konstruktoru třídy. Členové výčtu MethodImplOptions odpovídají bitovým polím v CorMethodImpl
tabulce metadat. To znamená, že informace o atributu nelze načíst za běhu voláním MemberInfo.GetCustomAttributes metody. Místo toho se načte voláním metody MethodInfo.GetMethodImplementationFlags
nebo ConstructorInfo.GetMethodImplementationFlags
metody.
Konstruktory
MethodImplAttribute() |
Inicializuje novou instanci MethodImplAttribute třídy. |
MethodImplAttribute(Int16) |
Inicializuje novou instanci MethodImplAttribute třídy se zadanou MethodImplOptions hodnotou. |
MethodImplAttribute(MethodImplOptions) |
Inicializuje novou instanci MethodImplAttribute třídy se zadanou MethodImplOptions hodnotou. |
Pole
MethodCodeType |
Hodnota MethodCodeType označující, jaký druh implementace je k dispozici pro tuto metodu. |
Vlastnosti
TypeId |
Při implementaci v odvozené třídě získá jedinečný identifikátor pro tento Attribute. (Zděděno od Attribute) |
Value |
MethodImplOptions Získá hodnotu popisující atribut metodu. |
Metody
Equals(Object) |
Vrací hodnotu, která určuje, zda je tato instance rovna zadanému objektu. (Zděděno od Attribute) |
GetHashCode() |
Vrátí hodnotu hash pro tuto instanci. (Zděděno od Attribute) |
GetType() |
Type Získá aktuální instanci. (Zděděno od Object) |
IsDefaultAttribute() |
Při přepsání v odvozené třídě určuje, zda hodnota této instance je výchozí hodnotou odvozené třídy. (Zděděno od Attribute) |
Match(Object) |
Při přepsání v odvozené třídě vrátí hodnotu, která označuje, zda se tato instance rovná zadanému objektu. (Zděděno od Attribute) |
MemberwiseClone() |
Vytvoří použádnou kopii aktuálního souboru Object. (Zděděno od Object) |
ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Explicitní implementace rozhraní
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Mapuje sadu názvů na odpovídající sadu identifikátorů pro rozesílání. (Zděděno od Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Načte informace o typu objektu, který lze použít k získání informací o typu rozhraní. (Zděděno od Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
Získá počet rozhraní typu informací, které objekt poskytuje (0 nebo 1). (Zděděno od Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Poskytuje přístup k vlastnostem a metodám vystaveným objektem. (Zděděno od Attribute) |