CA2132:預設建構函式至少必須和基底型別的預設建構函式一樣關鍵

型別名稱

DefaultConstructorsMustHaveConsistentTransparency

CheckId

CA2132

分類

Microsoft.Security

中斷變更

中斷

注意事項注意事項

這個警告只會套用至執行 CoreCLR (專屬於 Silverlight Web 應用程式的 CLR 版本) 的程式碼。

原因

衍生類別之預設建構函式的透明度屬性重要性不如基底類別的透明度。

規則描述

Silverlight 應用程式程式碼無法使用擁有 SecurityCriticalAttribute 的型別及成員。 重視安全性的型別以及成員,只能夠由 .NET Framework for Silverlight 類別庫中的受信任程式碼使用。 由於衍生類別中的公用或受保護建構所具有的透明度必須大於或等於其基底類別,因此應用程式中的類別不可衍生自標記為 SecurityCritical 的類別。

針對 CoreCLR 平台程式碼,如果基底型別具有公用或保護的非透明預設建構函式,則衍生型別必須遵守預設建構函式的繼承規則。 衍生型別也必須具有預設建構函式,而且該建構函式的重要性必須至少為基底型別的預設建構函式。

如何修正違規

若要修正違規情形,請移除型別,或者不要從安全性不透明型別衍生。

隱藏警告的時機

不隱藏此規則的警告。 應用程式程式碼違反這項規則會導致 CoreCLR 拒絕載入含有 TypeLoadException 的型別。

程式碼

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() { }
    }
}