Is the class NativeMethods handled specially in .NET?

Bryan picture Bryan · Sep 18, 2009 · Viewed 10.4k times · Source

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

Answer

Pete OHanlon picture Pete OHanlon · Sep 18, 2009

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.