当您需要调用非托管代码时使用它.
例如,您可能需要调用Windows API函数,因此您可以声明如下所示:
[DllImport("Advapi32.dll", EntryPoint="GetUserName", ExactSpelling=false,
SetLastError=true)]
static extern bool GetUserName(
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize );
Run Code Online (Sandbox Code Playgroud)
参考文献:http: //www.csharphelp.com/2006/01/call-unmanaged-code-part-1-simple-dllimport/