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
實作旗標要設定。
例外狀況
包含型態先前是使用 CreateType()。
-或-
對於目前的方法,性質 IsGenericMethod 為 true,但性質 IsGenericMethodDefinition 為 false。
範例
以下程式碼範例說明了該方法在情境上的使用 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 ...
備註
當你將此方法與該方法結合SetCustomAttribute使用SetImplementationFlags時,請注意可能存在的互動。 例如,使用 SetCustomAttribute 該方法加入 DllImportAttribute 屬性也會設定 MethodImplAttributes.PreserveSig 該標誌。 如果你之後呼叫該 SetImplementationFlags 方法,該 PreserveSig 標記會被覆寫。 有兩種方法可以避免這種情況:
先先呼叫方法, SetImplementationFlags 再呼叫 SetCustomAttribute 方法。 此 SetCustomAttribute 方法始終尊重現有的方法實作標誌。
當你設定實作旗標時,先呼叫該 GetMethodImplementationFlags 方法來取得現有的旗標,然後用位元順序 OR 加入你的標誌,然後再呼叫這個 SetImplementationFlags 方法。