MethodImplAttribute Class
Updated: September 2010
Specifies the details of how a method is implemented. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
You can apply this attribute to methods or constructors.
This attribute lets you customize the configuration of the method or constructor to which it applies. The members of the MethodImplOptions enumeration correspond to bit fields in the CorMethodImpl metadata table. This means that information on the attribute cannot be retrieved at run time by calling the MemberInfo.GetCustomAttributes method; instead, it is retrieved by calling either the MethodInfo.GetMethodImplementationFlags or the ConstructorInfo.GetMethodImplementationFlags method.
The following example applies the MethodImplAttribute to the GetCalendarName method to ensure that it is not inlined at run time by the just-in-time (JIT) compiler.
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
The following example then calls the MethodInfo.GetMethodImplementationFlags method to determine which flags are set for the GetCalendarName method. It also demonstrates that this information is not retrieved by the MemberInfo.GetCustomAttributes(System.Boolean) method.
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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.