MethodBuilder.SetImplementationFlags(MethodImplAttributes) Метод

Определение

Задает флаги реализации для этого метода.

public:
 void SetImplementationFlags(System::Reflection::MethodImplAttributes attributes);
public void SetImplementationFlags (System.Reflection.MethodImplAttributes attributes);
member this.SetImplementationFlags : System.Reflection.MethodImplAttributes -> unit
Public Sub SetImplementationFlags (attributes As MethodImplAttributes)

Параметры

attributes
MethodImplAttributes

Флаги реализации для установки.

Исключения

Содержащий тип был создан ранее с помощью CreateType().

-или-

Для текущего метода свойство IsGenericMethod имеет значение true, но свойство IsGenericMethodDefinition имеет значение false.

Примеры

В приведенном ниже примере кода показано контекстное использование SetImplementationFlags метода для описания реализации MSIL в теле метода.

array<Type^>^ temp0 = { int::typeid, int::typeid };
MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "MyMethod",
                               MethodAttributes::Public,
                               CallingConventions::HasThis,
                               int::typeid,
                               temp0 );

// 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)(
                                        MethodImplAttributes::IL |
                                        MethodImplAttributes::Managed |
                                        MethodImplAttributes::Synchronized |
                                        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 ...
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 ...

Комментарии

При использовании метода в сочетании SetImplementationFlags с методом SetCustomAttribute следует учитывать потенциальные взаимодействия. Например, использование SetCustomAttribute метода для добавления атрибута DllImportAttribute также задает MethodImplAttributes.PreserveSig флаг . При последующем вызове SetImplementationFlags метода PreserveSig флаг перезаписывается. Этого можно избежать двумя способами:

  • Вызовите SetImplementationFlags метод перед вызовом SetCustomAttribute метода . Метод SetCustomAttribute всегда учитывает существующие флаги реализации метода.

  • При установке флагов реализации вызовите GetMethodImplementationFlags метод , чтобы получить существующие флаги, используйте побитовое ИЛИ для добавления флага SetImplementationFlags , а затем вызовите метод .

Применяется к