Possible Duplicate:
Is the class NativeMethods handled specially in .NET?
我正在开发一个 C# 项目,该项目为某些功能调用一些非托管 C++ 代码。我发现了 fxCop,它提示我从非托管库中包含函数的方式。
它建议使用此处解释的 NativeMethods 类之一:
http://msdn.microsoft.com/en-us/library/ms182161%28v=vs.80%29.aspx
我读过它,但我不能说我理解它到底做了什么以及使用它有什么好处。如果有人能向我解释“此类不抑制非托管代码许可的堆栈遍历”的含义,我将非常高兴。和“此类抑制非托管代码权限的堆栈遍历。”
编辑:我真正想知道的是它如何影响我的代码。我已经阅读了它在 MSDN 上所说的内容,显然它对某些人来说已经足够了,但对我来说太高了,无法理解。这就是为什么我要求进行一些详细说明。
本文的 Visual Studio 2012 版本包含更多解释,
http://msdn.microsoft.com/en-us/library/ms182161(v=vs.110).aspx
To fix a violation of this rule, move the method to the appropriate
NativeMethods class. For most applications, moving P/Invokes to a new
class that is named NativeMethods is enough.
However, if you are developing libraries for use in other
applications, you should consider defining two other classes that are
called SafeNativeMethods and UnsafeNativeMethods. These classes
resemble the NativeMethods class; however, they are marked by using a
special attribute called SuppressUnmanagedCodeSecurityAttribute. When
this attribute is applied, the runtime does not perform a full stack
walk to make sure that all callers have the UnmanagedCode permission.
The runtime ordinarily checks for this permission at startup. Because
the check is not performed, it can greatly improve performance for
calls to these unmanaged methods, It also enables code that has
limited permissions to call these methods.
However, you should use this attribute with great care. It can have
serious security implications if it is implemented incorrectly..
For information about how to implement the methods, see the
NativeMethods Example, SafeNativeMethods Example, and
UnsafeNativeMethods Example.