virtualCERCall MDA

更新:2007 年 11 月

virtualCERCall Managed 偵錯助理 (MDA) 會以警告的形式啟動,指示限制的執行區域 (CER) 呼叫圖形內的呼叫位置會參考虛擬目標,也就是對非 final 虛擬方法的虛擬呼叫或使用介面的呼叫。Common Language Runtime (CLR) 無法單從中繼語言 (Intermediate Language) 和中繼資料分析來預測這些呼叫的目標方法;因此,無法將呼叫樹狀圖準備為 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();
    }
}

請參閱

概念

診斷 Managed 偵錯助理的錯誤

Interop 封送處理概觀

參考

MarshalAsAttribute