SYSLIB0004: 制約された実行領域 (CER) 機能はサポートされていません

制約された実行領域 (CER) 機能は、.NET Framework でのみサポートされています。 そのため、さまざまな CER 関連 API は、.NET 5 以降では、古い形式としてマークされています。 これらの API を使用すると、コンパイル時に警告 SYSLIB0004 が生成されます。

次の CER 関連の API は旧型式です。

ただし、次の CER 関連の API は旧型式では "ありません"。

問題回避

  • CER 属性をメソッドに適用している場合、その属性を削除します。 そのような属性は、.NET 5 以降のバージョンで効果がありません。

    // REMOVE the attribute below.
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    public void DoSomething()
    {
    }
    
    // REMOVE the attribute below.
    [PrePrepareMethod]
    public void DoSomething()
    {
    }
    
  • RuntimeHelpers.ProbeForSufficientStack または RuntimeHelpers.PrepareContractedDelegate を呼び出している場合、呼び出しを削除します。 そのような呼び出しは、.NET 5 以降のバージョンで効果がありません。

    public void DoSomething()
    {
        // REMOVE the call below.
        RuntimeHelpers.ProbeForSufficientStack();
    
        // (Remainder of your method logic here.)
    }
    
  • RuntimeHelpers.PrepareConstrainedRegions を呼び出している場合、呼び出しを削除します。 この呼び出しは、.NET 5 以降のバージョンで効果がありません。

    public void DoSomething_Old()
    {
        // REMOVE the call below.
        RuntimeHelpers.PrepareConstrainedRegions();
        try
        {
            // try code
        }
        finally
        {
            // cleanup code
        }
    }
    
    public void DoSomething_Corrected()
    {
        // There is no call to PrepareConstrainedRegions. It's a normal try / finally block.
    
        try
        {
            // try code
        }
        finally
        {
            // cleanup code
        }
    }
    
  • RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup を呼び出している場合、呼び出しを標準の try/catch/finally ブロックに置き換えます。

    // The sample below produces warning SYSLIB0004.
    public void DoSomething_Old()
    {
        RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(MyTryCode, MyCleanupCode, null);
    }
    public void MyTryCode(object state) { /* try code */ }
    public void MyCleanupCode(object state, bool exceptionThrown) { /* cleanup code */ }
    
    // The corrected sample below does not produce warning SYSLIB0004.
    public void DoSomething_Corrected()
    {
        try
        {
            // try code
        }
        catch (Exception ex)
        {
            // exception handling code
        }
        finally
        {
            // cleanup code
        }
    }
    

警告を抑制する

古い API を使う必要がある場合は、コードまたはプロジェクト ファイルで警告を抑制することができます。

単一の違反だけを抑制するには、ソース ファイルにプリプロセッサ ディレクティブを追加して警告を無効にしてから、再度有効にします。

// Disable the warning.
#pragma warning disable SYSLIB0004

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0004

プロジェクトですべての SYSLIB0004 警告を抑制するには、プロジェクト ファイルに <NoWarn> プロパティを追加します。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0004</NoWarn>
  </PropertyGroup>
</Project>

詳細については、「警告を表示しない」を参照してください。

関連項目