代码分析 CA1060 修复
我的应用程序中有以下代码:
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
我从代码分析 (FxCop) 收到以下警告:
CA1060 : Microsoft.Design : 因为它是 P/Invoke 方法, 'IconHelper.GetWindowLong(IntPtr, int)' 应该在类中定义 命名为 NativeMethods、SafeNativeMethods 或 UnsafeNativeMethods。
有人能告诉我应该把他们分到哪个班级吗?我不知道它是 Native、SafeNative 还是 UnsafeNative。
I have the following code in my application:
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
I am getting the following warning from Code Analysis (FxCop):
CA1060 : Microsoft.Design : Because it is a P/Invoke method,
'IconHelper.GetWindowLong(IntPtr, int)' should be defined in a class
named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods.
Can someone tell me which class I should put them in? I don't know if it is Native, SafeNative, or UnsafeNative.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在此处了解有关此警告的详细信息:http://msdn.microsoft.com/en-us/library /ms182161.aspx。简而言之:
You have detailed information about this warning here: http://msdn.microsoft.com/en-us/library/ms182161.aspx. In short:
尝试将它们全部移动到 NativeMethod 类中,它将解决问题
修复后您的代码应该如下所示
请记住更改调用这些方法的所有位置
更改之前
应该是
Try moving them all into a NativeMethod class, it will solve the problem
Your code should look like this after fixing it
Remember to change all the places where you are calling these methods
Before change
should be
进行定义来抑制此警告。
您可以通过在配置文件(.csproj 文件)中
You can suppress this warning by defining
in the configuration file (.csproj file).