{"record":{"id":"17425fa85204820b","repo":"icsharpcode/ILSpy","slug":"expected-a-typedef-typeref-or-typespec-handle","errorCode":null,"errorMessage":"Expected a TypeDef, TypeRef or TypeSpec handle!","messagePattern":"Expected a TypeDef, TypeRef or TypeSpec handle!","errorType":"exception","errorClass":"BadImageFormatException","httpStatus":null,"severity":"error","filePath":"ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs","lineNumber":1555,"sourceCode":"\t\t\tWriteMetadataToken(output, module, handle, MetadataTokens.GetToken(handle),\r\n\t\t\t\tspaceAfter: true, spaceBefore: true, ShowMetadataTokens, ShowMetadataTokensInBase10);\r\n\t\t\tWriteFlags(eventDefinition.Attributes, eventAttributes, output);\r\n\t\t\tvar provider = new DisassemblerSignatureTypeProvider(module, output);\r\n\t\t\tAction<ILNameSyntax> signature;\r\n\t\t\tswitch (eventDefinition.Type.Kind)\r\n\t\t\t{\r\n\t\t\t\tcase HandleKind.TypeDefinition:\r\n\t\t\t\t\tsignature = provider.GetTypeFromDefinition(module.Metadata, (TypeDefinitionHandle)eventDefinition.Type, 0);\r\n\t\t\t\t\tbreak;\r\n\t\t\t\tcase HandleKind.TypeReference:\r\n\t\t\t\t\tsignature = provider.GetTypeFromReference(module.Metadata, (TypeReferenceHandle)eventDefinition.Type, 0);\r\n\t\t\t\t\tbreak;\r\n\t\t\t\tcase HandleKind.TypeSpecification:\r\n\t\t\t\t\tsignature = provider.GetTypeFromSpecification(module.Metadata, new MetadataGenericContext(declaringType, module),\r\n\t\t\t\t\t\t(TypeSpecificationHandle)eventDefinition.Type, 0);\r\n\t\t\t\t\tbreak;\r\n\t\t\t\tdefault:\r\n\t\t\t\t\tthrow new BadImageFormatException(\"Expected a TypeDef, TypeRef or TypeSpec handle!\");\r\n\t\t\t}\r\n\t\t\tsignature(ILNameSyntax.TypeName);\r\n\t\t\toutput.Write(' ');\r\n\t\t\toutput.Write(DisassemblerHelpers.Escape(module.Metadata.GetString(eventDefinition.Name)));\r\n\t\t}\r\n\t\t#endregion\r\n\r\n\t\t#region Disassemble Type\r\n\t\tinternal static readonly EnumNameCollection<TypeAttributes> typeVisibility = new EnumNameCollection<TypeAttributes>() {\r\n\t\t\t{ TypeAttributes.Public, \"public\" },\r\n\t\t\t{ TypeAttributes.NotPublic, \"private\" },\r\n\t\t\t{ TypeAttributes.NestedPublic, \"nested public\" },\r\n\t\t\t{ TypeAttributes.NestedPrivate, \"nested private\" },\r\n\t\t\t{ TypeAttributes.NestedAssembly, \"nested assembly\" },\r\n\t\t\t{ TypeAttributes.NestedFamily, \"nested family\" },\r\n\t\t\t{ TypeAttributes.NestedFamANDAssem, \"nested famandassem\" },\r\n\t\t\t{ TypeAttributes.NestedFamORAssem, \"nested famorassem\" },\r\n\t\t};\r","sourceCodeStart":1537,"sourceCodeEnd":1573,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs#L1537-L1573","documentation":"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.","triggerScenarios":"Disassembling an event whose EventType column points at a handle that is not a TypeDef/Ref/Spec: truncated or malformed metadata.","commonSituations":"Corrupt or obfuscated assemblies; PE files produced by broken tooling.","solutions":["Catch BadImageFormatException around DisassembleEvent and skip the offending event.","Validate the assembly with peverify and obtain a clean copy."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    disassembler.DisassembleEvent(module, eventHandle);\n} catch (BadImageFormatException ex) {\n    logger.Warning(\"Corrupt event metadata in {Module}: {Message}\", module.FileName, ex.Message);\n}","preventionTips":["Treat BadImageFormatException while disassembling events as corrupt input.","Validate assemblies with peverify before batch disassembly."],"tags":["disassembly","il","bad-metadata","events","metadata"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}