我的代码导入以下本地方法:
当我对代码进行代码分析时,我会收到警告,例如:
我尝试将这些导入放入静态的
我没有尝试将它们放入
老实说,我非常困惑这个警告的真正含义以及抑制它的不同选项的后果。
有人能给我解释一下吗?
编辑:
我的代码针对.NET 2.0框架。
程序集标记为
- 来自gdi32.dll的
DeleteObject
、GetFontData
和SelectObject
- 来自user32.dll的
GetDC
和ReleaseDC
当我对代码进行代码分析时,我会收到警告,例如:
有人能用通俗易懂的语言解释一下这个警告的真正含义吗?CA5122 P/Invoke declarations should not be safe-critical.
P/Invoke method '
GdiFont.DeleteObject(IntPtr)
' is marked safe-critical. Since P/Invokes may only be called by critical code, this declaration should either be marked as security critical, or have its annotation removed entirely to avoid being misleading.
我尝试将这些导入放入静态的
SafeNativeMethods
类中作为内部静态方法,但这并不能消除警告。我没有尝试将它们放入
NativeMethods
中,因为在阅读了这篇文章之后,我不确定这是正确的方法,因为我不想让我的代码在中等信任环境下完全无法使用(我认为这将是将导入移动到NativeMethods
的后果)。老实说,我非常困惑这个警告的真正含义以及抑制它的不同选项的后果。
有人能给我解释一下吗?
编辑:
我的代码针对.NET 2.0框架。
程序集标记为
[assembly: AllowPartiallyTrustedCallers]
方法的声明方式如下:
[DllImport("gdi32")]
internal static extern int DeleteObject(HANDLE hObject);