CA2135:级别 2 程序集不应包含 LinkDemand

类型名

SecurityRuleSetLevel2MethodsShouldNotBeProtectedWithLinkDemands

CheckId

CA2135

类别

Microsoft.Security

是否重大更改

原因

类或类成员使用使用了第 2 级安全机制的应用程序中的 LinkDemand

规则说明

在级别为 2 的安全规则集中已弃用 LinkDemand。 将方法、类型和字段标记为 SecurityCriticalAttribute 特性,以代替 LinkDemands 在实时 (JIT) 编译时进行强制安全检查。

如何解决冲突

要修复该规则的冲突,删除 LinkDemand 并使用 SecurityCriticalAttribute 特性标记类型或成员。

何时禁止显示警告

不要禁止显示此规则发出的警告。

示例

在下面的示例中,LinkDemand 将被删除,方法标记为 SecurityCriticalAttribute 特性。

using System;
using System.Security;
using System.Security.Permissions;

namespace TransparencyWarningsDemo
{

    public class MethodsProtectedWithLinkDemandsClass
    {
        // CA2135 violation - the LinkDemand should be removed, and the method marked [SecurityCritical] instead
        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        public void ProtectedMethod()
        {
        }
    }
}