次の方法で共有


CA2132: 既定のコンストラクターは、基本型の既定コンストラクターと同程度以上、重要であることが必要

TypeName

DefaultConstructorsMustHaveConsistentTransparency

CheckId

CA2132

[カテゴリ]

Microsoft.Security

互換性に影響する変更点

あり

[!メモ]

この警告は、CoreCLR (Silverlight Web アプリケーションに固有の CLR バージョン) を実行しているコードにのみ適用されます。

原因

派生クラスの既定のコンストラクターの透過性属性が、基本クラスの透過性と同程度に重要ではありません。

規則の説明

SecurityCriticalAttribute を持つ型やメンバーを Silverlight アプリケーション コードで使用することはできません。セキュリティが重要な型やメンバーは、.NET Framework for Silverlight クラス ライブラリの信頼されているコードからのみ使用できます。派生クラスにおけるパブリックな構築または保護された構築の透過性は、基本クラスと同程度以上である必要があるため、アプリケーション内のクラスを、SecurityCritical としてマークされたクラスから派生させることはできません。

CoreCLR プラットフォーム コードでは、パブリックな、または保護された透過的でない既定のコンストラクターが基本型にある場合、派生型は既定のコンストラクターの継承規則に従う必要があります。つまり、派生型でも既定のコンストラクターを設定する必要があり、そのコンストラクターは基本型の既定のコンストラクターと同程度以上に重要である必要があります。

違反の修正方法

この違反を修正するには、型を削除するか、セキュリティ上透過的でない型から派生させないようにします。

警告を抑制する状況

この規則による警告を抑制しないでください。アプリケーション コードでこの規則に違反すると、TypeLoadException が発生し、CoreCLR による型の読み込みが拒否されます。

Dd983956.collapse_all(ja-jp,VS.110).gifコード

using System;
using System.Security;

namespace TransparencyWarningsDemo
{

    public class BaseWithSafeCriticalDefaultCtor
    {
        [SecuritySafeCritical]
        public BaseWithSafeCriticalDefaultCtor() { }
    }

    public class DerivedWithNoDefaultCtor : BaseWithSafeCriticalDefaultCtor
    {
        // CA2132 violation - since the base has a public or protected non-transparent default .ctor, the
        // derived type must also have a default .ctor
    }

    public class DerivedWithTransparentDefaultCtor : BaseWithSafeCriticalDefaultCtor
    {
        // CA2132 violation - since the base has a safe critical default .ctor, the derived type must have
        // either a safe critical or critical default .ctor.  This is fixed by making this .ctor safe critical
        // (however, user code cannot be safe critical, so this fix is platform code only).
        DerivedWithTransparentDefaultCtor() { }
    }

    public class BaseWithCriticalCtor
    {
        [SecurityCritical]
        public BaseWithCriticalCtor() { }
    }

    public class DerivedWithSafeCriticalDefaultCtor : BaseWithSafeCriticalDefaultCtor
    {
        // CA2132 violation - since the base has a critical default .ctor, the derived must also have a critical
        // default .ctor.  This is fixed by making this .ctor critical, which is not available to user code
        [SecuritySafeCritical]
        public DerivedWithSafeCriticalDefaultCtor() { }
    }
}