NativeMethods 类是否在 .NET 中进行了特殊处理?
https://msdn.microsoft.com/en- us/library/ms182161.aspx
此页面上描述的三个类是否在 .NET Framework 中进行了特殊处理? (NativeMethods、SafeNativeMethods 和 UnsafeNativeMethods)
我问的原因是我想知道是否可以创建 NativeMethods 类的类别。例如:
ComNativeMethods
User32NativeMethods
OleStorageNativeMethods
https://msdn.microsoft.com/en-us/library/ms182161.aspx
Are the three classes described on this paged handled specially in the .NET Framework? (NativeMethods, SafeNativeMethods and UnsafeNativeMethods)
The reason I'm asking is I'm wondering if it is alright to create categories of NativeMethods classes. For example:
ComNativeMethods
User32NativeMethods
OleStorageNativeMethods
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是约定,而不是要求。如果您反映到 CLR 并查看其中的代码,您通常会在 NativeMethods 类中看到 P/Invoke 代码。我相信,如果 FxCop 遇到它,它会建议将您的 P/Invoke 代码放入这样的类中。
It's a convention, not a requirement. If you reflect into the CLR and take a look at code in there, you'll often see P/Invoke code inside a NativeMethods class. I believe that FxCop will recommend putting your P/Invoke code in a class like this if it encounters it.
这只是一个约定,规定您应该将 p/invoke 方法放置在名为 *NativeMethods 的类中,但没有技术限制阻止您按照自己的方式进行操作...
It's just a convention that says you should place p/invoke methods in classes named *NativeMethods, but there is no technical constraint to prevent you from doing it your own way...
您可以用这种方式命名您的类,但您将继续收到代码分析警告 CA1060。此警告表明您没有遵守约定。因此,为了防止出现此警告,在命名具有 P/Invoke 方法的类时需要遵循约定。如果要对 P/Invoke 方法进行分类,可以使用命名空间。例如:
You can name your classes that way, but you will continue to get the code analysis warning CA1060. This warning indicates you are not following the convention. So to prevent this warning, you need to follow the convention when naming classes that have P/Invoke methods. If you want to categorize your P/Invoke methods, you can use namespaces. For example:
它们不是由 CLR 专门处理的。建议的做法是将 P/Invoke 放在名为 NativeMethods、SafeNativeMethods 或 UnsafeNativeMethods 的类中。
如果您在程序集上运行 FxCop,您将看到此建议发挥作用。
They aren't handled specially by the CLR. It's simply recommended practice to have your P/Invokes inside a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods.
You'll see this recommendation come into play if you run FxCop on your assemblies.