共用方式為


virtualCERCall MDA

注意

本文專屬於 .NET Framework。 它不適用於較新的 .NET 實作,包括 .NET 6 和更新版本。

virtualCERCall Managed 偵錯助理 (MDA) 會以警告模式啟動,指示限制的執行區域 (CER) 呼叫圖形內的呼叫位置會參考虛擬目標,也就是對非 final 虛擬方法的虛擬呼叫或使用介面的呼叫。 通用語言執行平台 (CLR) 無法單從中繼語言和中繼資料分析來預測這些呼叫的目標方法。 因此,無法將呼叫樹狀結構準備為 CER 圖形的一部分,而且無法自動封鎖在該樹狀子目錄內中止的執行緒。 這個 MDA 會在下列情況下發出警告:在執行階段已知計算呼叫目標所需的其他資訊時,可能需要使用 PrepareMethod 方法的明確呼叫來擴充 CER。

徵兆

不會在中止執行緒或卸載應用程式定義域時執行的 CER。

原因

CER 包含了對無法自動準備之虛擬方法的呼叫。

解決方法

為虛擬方法呼叫 PrepareMethod

對執行階段的影響

此 MDA 對 CLR 沒有影響。

輸出

Method 'MethodWithCer', while executing within a constrained execution region, makes a call
at IL offset 0x0024 to 'VirtualMethod', which is virtual and cannot be prepared automatically
at compile time. The caller must ensure this method is prepared explicitly at
runtime before entering the constrained execution region.
method name="VirtualMethod"
declaringType name="VirtualCERCall+MyClass"
  declaringModule name="mda"
    callsite name="MethodWithCer" offset="0x0024"

組態

<mdaConfig>
  <assistants>
    <VirtualCERCall />
  </assistants>
</mdaConfig>

範例

class MyClass
{
    [ReliabilityContract(Consistency.MayCorruptProcess, CER.None)]
    virtual void VirtualMethod()
    {
        ...
    }
}

class MyDerivedClass : MyClass
{
    [ReliabilityContract(Consistency.MayCorruptProcess, CER.None)]
    override void VirtualMethod()
    {
        ...
    }
}

void MethodWithCer(MyClass object)
{
    RuntimeHelpers.PrepareConstrainedRegions();
    try
    {
        ...
    }
    finally
    {
        // Start of the CER.

        // Cannot tell at analysis time whether object is a MyClass
        // or a MyDerivedClass, so we do not know which version of
        // VirtualMethod we are going to call.
        object.VirtualMethod();
    }
}

另請參閱