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 的實作。
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 ...
備註
當您將 方法與 SetCustomAttribute 方法搭配使用 SetImplementationFlags 時,請注意潛在的互動。 例如,使用 SetCustomAttribute 方法來新增 DllImportAttribute 屬性也會設定 MethodImplAttributes.PreserveSig 旗標。 如果您後續呼叫 SetImplementationFlags 方法,則會 PreserveSig 覆寫 旗標。 避免這種情形的做法有二種:
SetImplementationFlags呼叫 方法之前,請先呼叫 SetCustomAttribute 方法。 方法 SetCustomAttribute 一律會遵守現有的方法實作旗標。
當您設定實作旗標時,請呼叫 GetMethodImplementationFlags 方法來擷取現有的旗標、使用位 OR 來新增旗標,然後呼叫 SetImplementationFlags 方法。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應