TypeName |
|
CheckId |
CA2151 |
类别 |
Microsoft.Security |
是否重大更改 |
重大 |
原因
声明了安全透明字段或可靠关键字段。其类型被指定为安全关键。例如:
[assembly: AllowPartiallyTrustedCallers]
[SecurityCritical]
class Type1 { } // Security Critical type
class Type2 // Security transparent type
{
Type1 m_field; // CA2151, transparent field of critical type
}
在此示例中,m_field 是一个安全关键类型的安全透明字段。
规则说明
若要使用安全关键类型,引用该类型的代码必须是安全关键或安全可靠关键。即使引用是间接的,也需如此。例如,当你引用具有关键类型的透明字段时,你的代码必须是安全关键的或安全可靠的。因此,具有安全透明字段或安全可靠关键字段具有误导性,因为透明代码仍然无法访问该字段。
如何解决冲突
若要解决此规则的冲突,请使用 SecurityCriticalAttribute 特性标记该字段,或将该字段引用的类型设为安全透明或安全关键。
// Fix 1: Make the referencing field security critical
[assembly: AllowPartiallyTrustedCallers]
[SecurityCritical]
class Type1 { } // Security Critical type
class Type2 // Security transparent type
{
[SecurityCritical]
Type1 m_field; // Fixed: critical type, critical field
}
// Fix 2: Make the referencing field security critical
[assembly: AllowPartiallyTrustedCallers]
class Type1 { } // Type1 is now transparent
class Type2 // Security transparent type
{
[SecurityCritical]
Type1 m_field; // Fixed: critical type, critical field
}
何时禁止显示警告
不禁止显示此规则发出的警告。
代码
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace TransparencyWarningsDemo
{
public class SafeNativeMethods
{
// CA2145 violation - transparent method marked SuppressUnmanagedCodeSecurity. This should be fixed by
// marking this method SecurityCritical.
[DllImport("kernel32.dll", SetLastError = true)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool Beep(uint dwFreq, uint dwDuration);
}
}