代码分析 CA1060 修复

发布于 2024-12-10 17:53:50 字数 843 浏览 3 评论 0原文

我的应用程序中有以下代码:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戒ㄋ 2024-12-17 17:53:50

您可以在此处了解有关此警告的详细信息:http://msdn.microsoft.com/en-us/library /ms182161.aspx。简而言之:

对于大多数应用程序,将 P/Invokes 移至名为 NativeMethods 的新类就足够了。

You have detailed information about this warning here: http://msdn.microsoft.com/en-us/library/ms182161.aspx. In short:

For most applications, moving P/Invokes to a new class that is named NativeMethods is enough.

怀中猫帐中妖 2024-12-17 17:53:50

尝试将它们全部移动到 NativeMethod 类中,它将解决问题

修复后您的代码应该如下所示

public class NativeMethods {
[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);
}

请记住更改调用这些方法的所有位置

更改之前

SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam)

应该是

NativeMethods.SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam)

Try moving them all into a NativeMethod class, it will solve the problem

Your code should look like this after fixing it

public class NativeMethods {
[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);
}

Remember to change all the places where you are calling these methods

Before change

SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam)

should be

NativeMethods.SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam)
鸩远一方 2024-12-17 17:53:50

进行定义来抑制此警告。

<PropertyGroup> 
..... 
     <NoWarn>CA1060</NoWarn>
..... 
</PropertyGroup>

您可以通过在配置文件(.csproj 文件)中

You can suppress this warning by defining

<PropertyGroup> 
..... 
     <NoWarn>CA1060</NoWarn>
..... 
</PropertyGroup>

in the configuration file (.csproj file).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文