简体   繁体   中英

Should I change Methods or class names exposed using PInvoke / ComImport

I am using some native functions in my c# code. So I have some enums, methods and classes. I have turned on Microsoft All rules for code-analysis. So when I build, I received warnings regarding names.

For example;

[Guid("0000000d-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IEnumSTATSTG

Warning: "CA1709:IdentifiersShouldBeCasedCorrectly" for IEnumSTATSTG

What I have did is that I have suppressed warning. But is it a good practice? What should I do? Can I rename this interface IEnumSTATSTG to a something else which doesn't give warning.

Similarly If I have a method which has parameters written in C/C++ style, then it also gave me a code-analysis warnings.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Dest"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "grf"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flags"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "pwcs"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "pstg")]
uint MoveElementTo(
    /* [string][in] */ string pwcsName,
    /* [unique][in] */ IStorage pstgDest,
    /* [string][in] */ string pwcsNewName,
    /* [in] */ uint grfFlags);

Here I have suppressed warnings for all the parameter names. Is it OK to rename these parameters like pwcsName to Name or something else?

Names are not important for p/invoke or COM calls - usually, anything is possible :-).

What's most important is:

  • parameters or struct fields marshaling definition: size, type, direction (in, out, ref), attributes, offset in structure, etc.
  • orders: interface method order, parameter order, field order, etc.
  • GUIDs (IID, CLSID, etc.) values
  • custom attributes like ComVisible, Progid, MarshalAs, ComInterfaceType, etc.

That being said, I personally always keep the names as closest as possible as the original ones (sometimes you have to rename for some technical reason) and add SuppressMessage like you do. If you look at the .NET Framework itself (using a tool such .NET Reflector or ILSpy), you will observe the same rule in general.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM