MethodBuilder.SetImplementationFlags(MethodImplAttributes) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
设置此方法的实现标志。
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
要设置的实现标志。
例外
示例
下面的代码示例演示了该方法的上下文用法 SetImplementationFlags ,以描述方法主体中 MSIL 的实现。
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 方法以检索现有标志,使用按位 OR 添加标志,然后调用 SetImplementationFlags 该方法。