Partager via


CA2135: Level 2 assemblies should not contain LinkDemands

Item Value
RuleId CA2135
Category Microsoft.Security
Breaking change Breaking

Cause

A class or class member is using a SecurityAction in an application that is using Level 2 security.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

LinkDemands are deprecated in the level 2 security rule set. Instead of using LinkDemands to enforce security at just-in-time (JIT) compilation time, mark the methods, types, and fields with the SecurityCriticalAttribute attribute.

How to fix violations

To fix a violation of this rule, remove the SecurityAction and mark the type or member with the SecurityCriticalAttribute attribute.

When to suppress warnings

Do not suppress a warning from this rule.

Example

In the following example, the SecurityAction should be removed and the method marked with the SecurityCriticalAttribute attribute.

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