Decompile a method marked [DllImport] and you get almost nothing back: an extern signature, a reconstructed attribute, and no body. Decompile a method marked [LibraryImport] and you get a page of real C# — fixed blocks, pointer casts, a nested call. Same native call, wildly different decompiled output. The reason is where the marshalling code lives, and it is worth understanding, because it changes what you can learn from an assembly by reading it.
The real question
Both attributes describe the same thing: a managed method that is really a call across the native boundary. So why does one survive decompilation as an empty husk and the other as a fully readable method? The answer is that classic [DllImport] defers all marshalling to the CLR at runtime, while [LibraryImport] — the source generator introduced in .NET 7 — writes the marshalling out as C# at compile time. One leaves the stub in the runtime; the other bakes it into your assembly.
Classic DllImport: a method with no body
Start with the old style:
[DllImport("user32.dll", EntryPoint = "MessageBoxW",
CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
In metadata this method is unusual. Its MethodDef row carries the pinvokeimpl flag, and it has no CIL. The declaration data lives in a separate metadata table, ImplMap, which links the method to a ModuleRef (user32.dll), the import name (MessageBoxW), and a set of flags encoding charset, calling convention, and SetLastError. When you open this in Glass, the IL view shows an empty method and the decompiler reconstructs the attribute from the ImplMap row:
// decompiled — the body is genuinely not here
[DllImport("user32.dll", EntryPoint = "MessageBoxW", ExactSpelling = true)]
internal static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
So where is the code that turns a string into a char* and pushes it onto the native stack? It does not exist in the assembly. The CLR builds a marshalling stub the first time the method is called, driven by the ImplMap flags and the signature. That stub is JIT-compiled in memory and thrown away when the process ends. A decompiler can never show it, because it was never on disk.
LibraryImport: the stub becomes C
Now the source-generated form. The method is partial, and the attribute lives on a declaration with no body:
[LibraryImport("user32.dll", EntryPoint = "MessageBoxW",
StringMarshalling = StringMarshalling.Utf16)]
internal static partial int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
At compile time, the LibraryImportGenerator supplies the other half of the partial method. It is ordinary C# — and it is what ends up in your assembly:
internal static unsafe partial int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType)
{
int __retVal;
fixed (char* __lpText_native = lpText)
fixed (char* __lpCaption_native = lpCaption)
{
__retVal = __PInvoke(hWnd, (ushort*)__lpText_native, (ushort*)__lpCaption_native, uType);
}
return __retVal;
// the real boundary crossing — a local function
[DllImport("user32.dll", EntryPoint = "MessageBoxW", ExactSpelling = true)]
static extern int __PInvoke(IntPtr hWnd, ushort* lpText, ushort* lpCaption, uint uType);
}
Read what the generator did. Because StringMarshalling.Utf16 matches the CLR's UTF-16 string layout, no copy is needed: the two strings are pinned with fixed, their pointers reinterpreted as ushort*, and passed straight through. The actual native call is a nested static extern local function — still a classic [DllImport], but now a private, compiler-named implementation detail. The public method is pure, visible marshalling glue.
What Glass actually shows
The nested [DllImport] local function does not keep a friendly name in the compiled assembly. C# lowers local functions to private static methods with a mangled name, so in metadata you will see something like <MessageBox>g__PInvoke|0_0. When you open the assembly in Glass, the decompiler recognises the mangling and renders it back as a local function inside MessageBox, close to the generated source above. Switch to the IL view and you can confirm the split directly:
.method assembly hidebysig static int32 MessageBox (...) cil managed
{
// real IL: ldarg, conv, the fixed-pin pattern, call to __PInvoke
}
.method assembly hidebysig static pinvokeimpl("user32.dll" ...) int32 '<MessageBox>g__PInvoke|0_0' (...) cil managed { }
Only the second method carries pinvokeimpl. That is the one true boundary crossing. Everything else — the pinning, the pointer cast, the return handling — is code the generator wrote and you can now read. For a UTF-8 string parameter the stub would be longer: it allocates a native buffer, calls into the UTF-8 marshaller to transcode, uses the buffer, then frees it in a finally. All of that, too, is decompilable, because all of it is real IL.
Why this matters when you read an assembly
This is not a curiosity — it changes what interop you can audit by inspection. With a classic [DllImport] surface, reading the assembly tells you the entry point and the declared signature, and nothing about how arguments are actually marshalled; the truth is in the CLR's runtime rules. With [LibraryImport], the marshalling is right there. You can see exactly which buffers are pinned, where a transcode allocates, whether a bool is marshalled as a 4-byte int or a 1-byte value, and when memory is freed. For reviewing an interop layer you have the rights to inspect — your own build, a dependency you are vetting, a crash you are debugging — the source-generated form is far more transparent.
It also tells you something about provenance. A library that uses [LibraryImport] was built for .NET 7 or later with the generator enabled, and it is AOT-friendly by design, precisely because the stub is static code rather than runtime-generated IL. Seeing the g__PInvoke methods in Glass is a quick signal of a modern, trimming- and AOT-ready interop layer.
Caveats
The decompiled stub is faithful C#, but it is the generator's C#, not yours — do not expect it to match hand-written marshalling you might write for the same call. Generated local-function names (g__PInvoke|0_0) are compiler artifacts and can shift between compiler versions, so don't treat them as a stable API. And the nested method is still a real [DllImport]: the final hop into native code is as opaque as ever — Glass shows you the managed marshalling, not the native function it lands in. If the assembly ships with a PDB, Glass can also recover the original argument names and make the stub even easier to follow; see recovering names from a PDB.
Takeaway
[DllImport] hides its marshalling in the runtime; [LibraryImport] writes it into your assembly. That single difference is why one decompiles to an empty husk and the other to a readable stub. When you need to understand how an interop call really moves its arguments across the boundary, open it in Glass.NET — and hope it was built with the generator. Glass is a free, open .NET decompiler with a VS-native UI, a CLI, and an MCP server.
Top comments (0)