DllImport vs Declare in VB.NET

后端 未结 4 1859
误落风尘
误落风尘 2020-12-14 03:51

I notice in the MSDN documentation that there are multiple ways to declare a reference to a function in an external DLL from within a VB.NET program.

The confusing

4条回答
  •  渐次进展
    2020-12-14 04:27

    In my opinion, since this keyword doesn't look deprected, etc. from what I searched, simply use compile-time keywords rather than attributes.

    Also, when you use the Declare, you don't need to write the End Function. The advantage of that is that you can create a whole module of declarations of function imports line by line, with no need to pulute your code with DllImports and End Functions.

    When you declare using the Declare keyword, the compiler treats this function as Shared anyway, so it can be accessed via other extenal objects.

    But I think in the current VB.NET they're both addressed to the same target and no performance difference - no warranty on this one.

    So my conclusion is: Do use the Declare instead of DllImport, especially reading what you quoted that Microsoft stated that it should be used in rare cases.

提交回复
热议问题