CA2132:默认构造函数必须至少与基类型默认构造函数具有同样的关键性

类型名

DefaultConstructorsMustHaveConsistentTransparency

CheckId

CA2132

类别

Microsoft.Security

是否重大更改

提示

此警告仅应用于运行 CoreCLR(特定于 Silverlight Web 应用程序的 CLR 版本)的代码。

原因

派生类的默认构造函数的透明特性不是和基类的透明度一样关键的。

规则说明

具有 SecurityCriticalAttribute 的类型和成员无法供 Silverlight 应用程序代码使用。 安全关键类型和成员只能供 .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() { }
    }
}