| 类型名 | PInvokesShouldNotBeVisible | 
| CheckId | CA1401 | 
| 类别 | Microsoft.Interoperability | 
| 是否重大更改 | 是 | 
原因
公共类型的公共或受保护的方法具有 System.Runtime.InteropServices.DllImportAttribute 特性(在 Visual Basic 中还可通过 Declare 关键字实现)。
规则说明
用 DllImportAttribute 特性标记的方法(或者在 Visual Basic 中使用 Declare 关键字定义的方法)使用平台调用服务访问非托管代码。 这些方法不能公开。 保持这些方法为私有或内部方法可以阻止调用方访问他们通过其他方式无法调用的非托管 API,从而确保您的库不会被用来破坏安全性。
如何解决冲突
要修复与该规则的冲突,请更改方法的访问级别。
何时禁止显示警告
不要禁止显示此规则发出的警告。
示例
下面的示例声明一个与该规则冲突的方法。
Imports System
NameSpace MSInternalLibrary
' Violates rule: PInvokesShouldNotBeVisible.
Public Class NativeMethods
    Public Declare Function RemoveDirectory Lib "kernel32"( _
        ByVal Name As String) As Boolean
End Class
End NameSpace 
using System;
using System.Runtime.InteropServices;
namespace InteroperabilityLibrary
{
    // Violates rule: PInvokesShouldNotBeVisible.
    public class NativeMethods
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        public static extern bool RemoveDirectory(string name);
    }
}