MethodImplAttributes 枚举


指定方法实现的属性的标志。Specifies flags for the attributes of a method implementation.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

此 API 支持产品基础结构,不能在代码中直接使用。

public enum class MethodImplAttributes
public enum MethodImplAttributes
type MethodImplAttributes = 
Public Enum MethodImplAttributes


AggressiveInlining 256

指定方法应尽可能内联。Specifies that the method should be inlined wherever possible.

AggressiveOptimization 512

指定应尽可能优化的方法。Specifies that the method should be optimized whenever possible.

CodeTypeMask 3

指定有关代码类型的标志。Specifies flags about code type.

ForwardRef 16

指定方法是未定义的。Specifies that the method is not defined.

IL 0

指定方法实现位于 Microsoft 中间语言 (MSIL) 中。Specifies that the method implementation is in Microsoft intermediate language (MSIL).

InternalCall 4096

指定内部调用。Specifies an internal call.

Managed 0

指定方法在托管代码中实现。Specifies that the method is implemented in managed code.

ManagedMask 4

指定方法是在托管代码还是非托管代码中实现。Specifies whether the method is implemented in managed or unmanaged code.

MaxMethodImplVal 65535

指定范围检查值。Specifies a range check value.

Native 1

指定方法实现为本机。Specifies that the method implementation is native.

NoInlining 8

指定方法不能内联。Specifies that the method cannot be inlined.

NoOptimization 64

指定在调试可能的代码生成问题时,方法不由实时 (JIT) 编译器或本机代码生成优化(请参阅 Ngen.exe)。Specifies that the method is not optimized by the just-in-time (JIT) compiler or by native code generation (see Ngen.exe) when debugging possible code generation problems.


指定方法实现位于优化中间语言 (OPTIL) 中。Specifies that the method implementation is in Optimized Intermediate Language (OPTIL).

PreserveSig 128

指定方法签名是完全按照声明导出的。Specifies that the method signature is exported exactly as declared.

Runtime 3

指定方法实现由运行时提供。Specifies that the method implementation is provided by the runtime.

SecurityMitigations 1024

指定 JIT 编译器应查找安全缓解属性,例如用户定义的 System.Runtime.CompilerServices.SecurityMitigationsAttributeSpecifies that the JIT compiler should look for security mitigation attributes, such as the user-defined System.Runtime.CompilerServices.SecurityMitigationsAttribute. JIT 编译器会应用任何找到的相关安全缓解措施。If found, the JIT compiler applies any related security mitigations. 从 .NET Framework 4.8 开始可用。Available starting with .NET Framework 4.8.

Synchronized 32

指定方法是通过正文的单线程方法。Specifies that the method is single-threaded through the body. 静态方法(在 Visual Basic 中为Shared )在类型上锁定,而实例方法在实例上锁定。Static methods (Shared in Visual Basic) lock on the type, whereas instance methods lock on the instance. 为此,也可以使用 C# lock 语句 或 Visual Basic SyncLock 语句You can also use the C# lock statement or the Visual Basic SyncLock statement for this purpose.

Unmanaged 4

指定方法在非托管代码中实现。Specifies that the method is implemented in unmanaged code.


使用按位 "或" 运算组合特性, 如下所示:The attributes are combined using the bitwise OR operation as follows:

代码实现掩码:Code implementation masks:

  • CodeTypeMask

  • IL

  • Native


  • Runtime

托管掩码:Managed masks:

  • ManagedMask

  • Unmanaged

  • Managed

实现信息和互操作掩码:Implementation information and interop masks:

  • ForwardRef

  • PreserveSig

  • InternalCall

  • Synchronized

  • NoInlining

  • NoOptimization

  • MaxMethodImplVal

  • SecurityMitigations(从 .NET Framework 4.8 开始可用。)SecurityMitigations (Available starting with .NET Framework 4.8.)


不建议对公共类型使用Synchronized锁定实例或类型上的锁定, 因为非自己的代码可以对公共类型和实例进行锁定。Locking on the instance or on the type, as with the Synchronized flag, is not recommended for public types, because code other than your own can take locks on public types and instances. 这可能会导致死锁或其他同步问题。This might cause deadlocks or other synchronization problems.