icsharpcode/ILSpy · error · BadImageFormatException

Unexpected rawTypeKind: {rawTypeKind} (0x{rawTypeKind:x})

Error message

Unexpected rawTypeKind: {rawTypeKind} (0x{rawTypeKind:x})

What it means

BadImageFormatException from DisassemblerSignatureTypeProvider.GetTypeFromDefinition when the rawTypeKind byte decoded from a type signature is not one of the valid ECMA-335 codes (0x00 default, 0x11 valuetype, 0x12 class). Any other byte means corrupt signature metadata.

Source

Thrown at ICSharpCode.Decompiler/Disassembler/DisassemblerSignatureTypeProvider.cs:241

				output.Write(']');
			};
		}

		public Action<ILNameSyntax> GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind)
		{
			return syntax => {
				switch (rawTypeKind)
				{
					case 0x00:
						break;
					case 0x11:
						output.Write("valuetype ");
						break;
					case 0x12:
						output.Write("class ");
						break;
					default:
						throw new BadImageFormatException($"Unexpected rawTypeKind: {rawTypeKind} (0x{rawTypeKind:x})");
				}
				((EntityHandle)handle).WriteTo(module, output, default);
			};
		}

		public Action<ILNameSyntax> GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind)
		{
			return syntax => {
				switch (rawTypeKind)
				{
					case 0x00:
						break;
					case 0x11:
						output.Write("valuetype ");
						break;
					case 0x12:
						output.Write("class ");
						break;

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Catch BadImageFormatException around the disassemble call and report the member as not disassemblable.
  2. Verify the assembly with peverify or obtain a fresh copy; if it reproducibly fails on valid metadata, file an ILSpy issue.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    disassembler.Disassemble(module, entityHandle, output);
} catch (BadImageFormatException ex) {
    logger.Warning("Corrupt signature in {Module}: {Message}", module.FileName, ex.Message);
}

Prevention

When it happens

Trigger: Disassembling IL (ildasm-style output) for an assembly whose type-signature encoding contains an invalid type-kind byte: truncated, hand-edited, or obfuscated metadata.

Common situations: Corrupt PE files; IL produced by buggy or obfuscating tools; a truncated download of the assembly.

Related errors


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