icsharpcode/ILSpy · error · BadImageFormatException

Implementation must either be an index into the File, Export

Error message

Implementation must either be an index into the File, ExportedType or AssemblyRef table.

What it means

BadImageFormatException while disassembling an ExportedType: its Implementation handle must index the File, ExportedType, or AssemblyRef table. Any other handle kind means corrupt ExportedType metadata, so the '.class extern' forwarding cannot be decoded.

Source

Thrown at ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs:2174

							if (declaringType.Implementation.Kind == HandleKind.ExportedType)
							{
								declaringType = metadata.GetExportedType((ExportedTypeHandle)declaringType.Implementation);
							}
							else
							{
								break;
							}
						}
						output.WriteLine();
						break;
					case HandleKind.AssemblyReference:
						output.Write(".assembly extern ");
						var reference = metadata.GetAssemblyReference((AssemblyReferenceHandle)exportedType.Implementation);
						output.Write(DisassemblerHelpers.Escape(metadata.GetString(reference.Name)));
						output.WriteLine();
						break;
					default:
						throw new BadImageFormatException("Implementation must either be an index into the File, ExportedType or AssemblyRef table.");
				}
				CloseBlock();
			}
			var moduleDefinition = metadata.GetModuleDefinition();

			output.WriteLine(".module {0}", metadata.GetString(moduleDefinition.Name));
			if (!skipMVID)
			{
				output.WriteLine("// MVID: {0}", metadata.GetGuid(moduleDefinition.Mvid).ToString("B").ToUpperInvariant());
			}

			if (module is PEFile peFile)
			{
				var headers = peFile.Reader.PEHeaders;
				output.WriteLine(".imagebase 0x{0:x8}", headers.PEHeader.ImageBase);
				output.WriteLine(".file alignment 0x{0:x8}", headers.PEHeader.FileAlignment);
				output.WriteLine(".stackreserve 0x{0:x8}", headers.PEHeader.SizeOfStackReserve);
				output.WriteLine(".subsystem 0x{0:x} // {1}", headers.PEHeader.Subsystem, headers.PEHeader.Subsystem.ToString());

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Catch BadImageFormatException around the module-header / ExportedType disassembly and continue with the rest.
  2. Validate with peverify and restore a clean copy of the assembly.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    disassembler.WriteAssemblyReferences(module); // includes ExportedType rows
} catch (BadImageFormatException ex) {
    logger.Warning("Corrupt ExportedType metadata in {Module}: {Message}", module.FileName, ex.Message);
}

Prevention

When it happens

Trigger: Disassembling a module whose .class extern (ExportedType) rows have an Implementation column pointing at an invalid table: malformed or hand-edited metadata.

Common situations: Corrupt or obfuscated assemblies; multi-module assemblies with damaged type-forwarding tables.

Related errors


AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13). Data as JSON: /api/errors/4c8cc1fc8e1ff693. Report an issue: GitHub.