Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MethodImplAttribute Class

Updated: September 2010

Specifies the details of how a method is implemented. This class cannot be inherited.

Namespace:  System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Constructor Or AttributeTargets.Method, Inherited := False)> _
Public NotInheritable Class MethodImplAttribute _
	Inherits Attribute
'Usage
Dim instance As MethodImplAttribute

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

System.Object
  System.Attribute
    System.Runtime.CompilerServices.MethodImplAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

September 2010

Revised extensively.

Customer feedback.

Community Additions

Show:
© 2017 Microsoft