icsharpcode/ILSpy · error · BadImageFormatException
Expected a TypeDef, TypeRef or TypeSpec handle!
Error message
Expected a TypeDef, TypeRef or TypeSpec handle!
What it means
BadImageFormatException while disassembling an event: the event's .type handle must be a TypeDefinition, TypeReference, or TypeSpecification. Any other HandleKind means corrupt EventMap/Event table metadata, so the signature cannot be decoded.
Source
Thrown at ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs:1555
WriteMetadataToken(output, module, handle, MetadataTokens.GetToken(handle),
spaceAfter: true, spaceBefore: true, ShowMetadataTokens, ShowMetadataTokensInBase10);
WriteFlags(eventDefinition.Attributes, eventAttributes, output);
var provider = new DisassemblerSignatureTypeProvider(module, output);
Action<ILNameSyntax> signature;
switch (eventDefinition.Type.Kind)
{
case HandleKind.TypeDefinition:
signature = provider.GetTypeFromDefinition(module.Metadata, (TypeDefinitionHandle)eventDefinition.Type, 0);
break;
case HandleKind.TypeReference:
signature = provider.GetTypeFromReference(module.Metadata, (TypeReferenceHandle)eventDefinition.Type, 0);
break;
case HandleKind.TypeSpecification:
signature = provider.GetTypeFromSpecification(module.Metadata, new MetadataGenericContext(declaringType, module),
(TypeSpecificationHandle)eventDefinition.Type, 0);
break;
default:
throw new BadImageFormatException("Expected a TypeDef, TypeRef or TypeSpec handle!");
}
signature(ILNameSyntax.TypeName);
output.Write(' ');
output.Write(DisassemblerHelpers.Escape(module.Metadata.GetString(eventDefinition.Name)));
}
#endregion
#region Disassemble Type
internal static readonly EnumNameCollection<TypeAttributes> typeVisibility = new EnumNameCollection<TypeAttributes>() {
{ TypeAttributes.Public, "public" },
{ TypeAttributes.NotPublic, "private" },
{ TypeAttributes.NestedPublic, "nested public" },
{ TypeAttributes.NestedPrivate, "nested private" },
{ TypeAttributes.NestedAssembly, "nested assembly" },
{ TypeAttributes.NestedFamily, "nested family" },
{ TypeAttributes.NestedFamANDAssem, "nested famandassem" },
{ TypeAttributes.NestedFamORAssem, "nested famorassem" },
};
View on GitHub (pinned to 60c08fcb74)
Solutions
- Catch BadImageFormatException around DisassembleEvent and skip the offending event.
- Validate the assembly with peverify and obtain a clean copy.
Defensive patterns
Strategy: try-catch
Try / catch
try {
disassembler.DisassembleEvent(module, eventHandle);
} catch (BadImageFormatException ex) {
logger.Warning("Corrupt event metadata in {Module}: {Message}", module.FileName, ex.Message);
} Prevention
- Treat BadImageFormatException while disassembling events as corrupt input.
- Validate assemblies with peverify before batch disassembly.
When it happens
Trigger: Disassembling an event whose EventType column points at a handle that is not a TypeDef/Ref/Spec: truncated or malformed metadata.
Common situations: Corrupt or obfuscated assemblies; PE files produced by broken tooling.
Related errors
- Unexpected rawTypeKind: {rawTypeKind} (0x{rawTypeKind:x})
- Implementation must either be an index into the File, Export
- Cannot get RVA section prefix from module
- types contains null element
- Could not find type definition {fullTypeName} in type system
AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13).
Data as JSON: /api/errors/17425fa85204820b.
Report an issue: GitHub.