Gendarme.Rules.Interoperability
From Mono
Gendarme's interoperability rules are located in the Gendarme.Rules.Interoperability.dll assembly. Latest sources are available from anonymous SVN (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Interoperability/) (tarball (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Interoperability.tar.gz?view=tar)).
| Table of contents |
Rules
CallGetLastErrorAfterPInvokeRule
Marshal.GetLastWin32Error() should be called directly after a PInvoke call.
Intermediate method calls, even managed, could overwrite the error code.
Bad example:
public void DestroyError () { MessageBeep (2); Console.WriteLine ("Beep"); int error = Marshal.GetLastWin32Error (); }
Good example:
public void GetError () { MessageBeep (2); int error = Marshal.GetLastWin32Error (); Console.WriteLine ("Beep"); } public void DontUseGetLastError () { MessageBeep (2); Console.WriteLine ("Beep"); }
MarshalStringsInPInvokeDeclarationsRule
This rule warns the developer if the CharSet has not been specified for string parameters of P/Invoke method, unless if they are individually decorated with [MarshalAs] attribute. This applies to any System.String and System.Text.StringBuilder parameters.
Bad example:
[DllImport("coredll.dll")] static extern int SHCreateShortcut (StringBuilder szShortcut, StringBuilder szTarget);
Good examples:
[DllImport("coredll.dll", CharSet = CharSet.Auto)] static extern int SHCreateShortcut (StringBuilder szShortcut, StringBuilder szTarget); [DllImport("coredll.dll")] static extern int SHCreateShortcut ([MarshalAs(UnmanagedType.LPTStr)] StringBuilder szShortcut, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder szTarget);
PInvokeShouldNotBeVisibleRule
This rule checks for PInvoke declaration methods that are visible outside their assembly.
Bad example:
[DllImport ("user32.dll")] public static extern bool MessageBeep (UInt32 beepType);
Good example:
[DllImport ("user32.dll")] internal static extern bool MessageBeep (UInt32 beepType);
UseManagedAlternativesToPInvokeRule
This rule warns the developer if certain external (P/Invoke) methods are being called in case they have managed alternatives provided by the .NET framework.
Bad example:
[DllImport ("kernel32.dll")] static extern void Sleep (uint dwMilliseconds); public void WaitTwoSeconds () { Sleep (2000); }
Good example:
public void WaitTwoSeconds () { System.Threading.Thread.Sleep (2000); }
Feedback
Please report any documentation errors, typos or suggestions to the Gendarme Google Group (http://groups.google.com/group/gendarme). Thanks!

Powered by MediaWiki