CA2136:成员不应有相互冲突的透明度注释

类型名

TransparencyAnnotationsShouldNotConflict

CheckId

CA2136

类别

Microsoft.Security

是否重大更改

原因

System.Security 安全特性(具有不同于该成员的容器的安全特性的透明效果)标记类型成员时,将触发此规则。

规则说明

将透明特性从较大作用域的代码元素应用到较小作用域的元素。 具有较大作用域的代码元素的透明特性优于第一个元素中包含的代码元素的透明特性。 例如,用 SecurityCriticalAttribute 特性标记的类不能包含用 SecuritySafeCriticalAttribute 特性标记的方法。

如何解决冲突

若要修复此冲突,从具有较低范围的码位元素中移除安全特性,或更改其要与包含代码元素相同的特性。

何时禁止显示警告

不要禁止显示与此规则有关的警告。

示例

在以下示例中,方法是用 SecuritySafeCriticalAttribute 特性标记的,该方法是标记为 SecurityCriticalAttribute 特性的类的成员。 应删除安全性安全特性。

using System;
using System.Security;

namespace TransparencyWarningsDemo
{

    [SecurityCritical]
    public class CriticalClass
    {
        // CA2136 violation - this method is not really safe critical, since the larger scoped type annotation
        // has precidence over the smaller scoped method annotation.  This can be fixed by removing the
        // SecuritySafeCritical attribute on this method
        [SecuritySafeCritical]
        public void SafeCriticalMethod()
        {
        }
    }
}