MethodBuilder.SetImplementationFlags Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets the implementation flags for this method.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub SetImplementationFlags ( _
    attributes As MethodImplAttributes _
)
[SecuritySafeCriticalAttribute]
public void SetImplementationFlags(
    MethodImplAttributes attributes
)

Parameters

Exceptions

Exception Condition
InvalidOperationException

The containing type was previously created using CreateType.

-or-

For the current method, the IsGenericMethod property is true, but the IsGenericMethodDefinition property is false.

Remarks

When you use the SetImplementationFlags method in combination with the SetCustomAttribute method, be aware of potential interactions. For example, using the SetCustomAttribute method to add the DllImportAttribute attribute also sets the MethodImplAttributes.PreserveSig flag. If you subsequently call the SetImplementationFlags method, the PreserveSig flag is overwritten. There are two ways to avoid this:

  • Call the SetImplementationFlags method before you call the SetCustomAttribute method. The SetCustomAttribute method always respects existing method implementation flags.

  • When you set implementation flags, call the GetMethodImplementationFlags method to retrieve the existing flags, use bitwise OR to add your flag, and then call the SetImplementationFlags method.

Examples

The following example illustrates the use of the SetImplementationFlags method to describe the implementation of MSIL in a method body.

Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
  MethodAttributes.Public, _
  CallingConventions.HasThis, _
  GetType(Integer), _
  New Type() {GetType(Integer), GetType(Integer)})

' Specifies that the dynamic method declared above has a an MSIL implementation,
' is managed, synchronized (single-threaded) through the body, and that it 
' cannot be inlined.

myMthdBuilder.SetImplementationFlags((MethodImplAttributes.IL Or _
   MethodImplAttributes.Managed Or _
   MethodImplAttributes.Synchronized Or _
   MethodImplAttributes.NoInlining))

' Create an ILGenerator for the MethodBuilder and emit MSIL here ...
      MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("MyMethod",
                     MethodAttributes.Public,
                     CallingConventions.HasThis,
                     typeof(int),
                     new Type[] { typeof(int),
                                 typeof(int) });

      // Specifies that the dynamic method declared above has a an MSIL implementation,
      // is managed, synchronized (single-threaded) through the body, and that it 
      // cannot be inlined.

      myMthdBuilder.SetImplementationFlags(MethodImplAttributes.IL |
                       MethodImplAttributes.Managed |
                       MethodImplAttributes.Synchronized |
                       MethodImplAttributes.NoInlining);

      // Create an ILGenerator for the MethodBuilder and emit MSIL here ...

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.