CA1415: P-Invoke를 올바르게 선언합니다.
항목 | 값 |
---|---|
RuleId | CA1415 |
범주 | Microsoft.Interoperability |
주요 변경 내용 | 호환성이 손상되지 않는 변경 - 매개 변수를 선언하는 P/Invoke를 어셈블리 외부에서 볼 수 없는 경우 호환성이 손상되는 변경 - 매개 변수를 선언하는 P/Invoke를 어셈블리 외부에서 볼 수 있는 경우 |
원인
플랫폼 호출 메서드가 잘못 선언되었습니다.
규칙 설명
플랫폼 호출 메서드는 관리되지 않는 코드에 액세스하고 Visual Basic 또는 System.Runtime.InteropServices.DllImportAttribute
위반 문제를 해결하는 방법
이 규칙의 위반 문제를 해결하려면 플랫폼 호출 메서드를 올바르게 선언합니다.
경고를 표시하지 않는 경우
이 규칙에서는 경고를 표시해야 합니다.
예시
다음 예제에서는 규칙을 위반하는 플랫폼 호출 메서드와 규칙을 충족하는 플랫폼 호출 메서드를 보여 줍니다.
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace InteroperabilityLibrary
{
// The platform invoke methods in this class violate the rule.
[ComVisible(true)]
internal class NativeMethods
{
private NativeMethods() { }
[DllImport("kernel32.dll", SetLastError = true)]
internal extern static uint ReadFile(
IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
IntPtr lpNumberOfBytesRead, IntPtr overlapped);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal extern static bool ReadFileEx(
IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
NativeOverlapped overlapped, IntPtr lpCompletionRoutine);
}
// The platform invoke methods in this class satisfy the rule.
[ComVisible(true)]
internal class UnsafeNativeMethods
{
private UnsafeNativeMethods() { }
//To compile this code, uncomment these lines and compile
//with "/unsafe".
//[DllImport("kernel32.dll", SetLastError = true)]
//unsafe internal extern static uint ReadFile(
// IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
// IntPtr lpNumberOfBytesRead, NativeOverlapped* overlapped);
//[DllImport("kernel32.dll", SetLastError = true)]
//[return: MarshalAs(UnmanagedType.Bool)]
//unsafe internal extern static bool ReadFileEx(
// IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
// NativeOverlapped* overlapped, IntPtr lpCompletionRoutine);
}
}
참고 항목
피드백
https://aka.ms/ContentUserFeedback을 참조하세요.
출시 예정: 2024년 내내 콘텐츠 피드백 메커니즘인 GitHub 문제를 단계적으로 폐지하고 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은다음에 대한 사용자 의견 제출 및 보기