Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

PInvokes should not be visible

TypeName

PInvokesShouldNotBeVisible

CheckId

CA1401

Category

Microsoft.Interoperability

Breaking Change

Breaking

A public or protected method in a public type has the System.Runtime.InteropServices.DllImportAttribute attribute (also implemented by the Declare keyword in Visual Basic).

Methods marked with the DllImportAttribute attribute (or methods defined using the Declare keyword in Visual Basic) use Platform Invocation Services to access unmanaged code. Such methods should not be exposed. Keeping these methods private or internal ensures that your library cannot be used to breach security by allowing callers access to unmanaged APIs they could not call otherwise.

To fix a violation of this rule, change the access level of the method.

Do not exclude a warning from this rule.

The following example declares a method that violates this rule.

using System;
using System.Runtime.InteropServices;

namespace InteroperabilityLibrary
{
    // Violates rule: PInvokesShouldNotBeVisible.
    public class NativeMethods
    {
        [DllImport("kernel32.dll")]
        public static extern bool RemoveDirectory(string name);
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft