{"record":{"id":"5b7ac2ddaafc5caa","repo":"icsharpcode/ILSpy","slug":"error-decompiling-metadatatokens-gettoken-entity","errorCode":null,"errorMessage":"Error decompiling @{MetadataTokens.GetToken(entity.MetadataToken):X8} {entity.FullName}","messagePattern":"Error decompiling @(.+?) (.+?)","errorType":"exception","errorClass":"DecompilerException","httpStatus":null,"severity":"error","filePath":"ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs","lineNumber":1921,"sourceCode":"\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\tdefault:\r\n\t\t\t\t\t\t\tthrow new ArgumentOutOfRangeException();\r\n\t\t\t\t\t}\r\n\t\t\t\t\tforeach (var item in typeDecl.Members)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (item is not EnumMemberDeclaration)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\titem.AddLeadingTrivia(new Comment(\" error: nested types are not permitted in C#.\"));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\treturn typeDecl;\r\n\t\t\t}\r\n\t\t\tcatch (Exception innerException) when (!(innerException is OperationCanceledException || innerException is DecompilerException))\r\n\t\t\t{\r\n\t\t\t\tthrow new DecompilerException(module, typeDef, innerException);\r\n\t\t\t}\r\n\t\t\tfinally\r\n\t\t\t{\r\n\t\t\t\tDecompilerEventSource.Log.DecompileTypeStop(typeDef);\r\n\t\t\t}\r\n\r\n\t\t\t// MemberIsHidden identifies event backing fields from the metadata name association\r\n\t\t\t// alone. When the event's accessors turn out not to be compiler-generated, the event\r\n\t\t\t// is decompiled with explicit accessors and no field-like declaration takes the\r\n\t\t\t// field's place, so the field must stay in the output even if no decompiled body\r\n\t\t\t// references it (referenced hidden members are re-added via the work list).\r\n\t\t\tbool IsBackingFieldOfNonAutomaticEvent(IEntity entity)\r\n\t\t\t{\r\n\t\t\t\tif (entity is not IField field || !settings.AutomaticEvents)\r\n\t\t\t\t\treturn false;\r\n\t\t\t\tif (!module.MetadataFile.PropertyAndEventBackingFieldLookup.IsEventBackingField((FieldDefinitionHandle)field.MetadataToken, out var eventHandle))\r\n\t\t\t\t\treturn false;\r\n\t\t\t\tif (AutoEventDecompiler.IsAutomaticEvent(typeSystem, module.GetDefinition(eventHandle), decompileRun, CancellationToken, out _))\r","sourceCodeStart":1903,"sourceCodeEnd":1939,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs#L1903-L1939","documentation":"DecompilerException wrapping any failure inside DoDecompile(ITypeDefinition). The catch re-throws non-cancellation, non-DecompilerException errors as DecompilerException(module, typeDef, inner) whose default message is 'Error decompiling @<token> <fullname>'. The InnerException is the real cause; this wrapper localizes the failure to one type so a batch decompile can continue with the rest.","triggerScenarios":"Any internal exception (bad IL, unsupported construct, transform bug, NullReferenceException on obfuscated metadata) thrown while decompiling a single type, when it is not already an OperationCanceledException or DecompilerException.","commonSituations":"Obfuscated/encrypted assemblies; assemblies produced by F#, ILASM, or older/preview compilers; types with malformed metadata; decompiler version gaps on new language features.","solutions":["Catch DecompilerException and inspect InnerException / StackTrace for the root cause.","Report the InnerException type and the failing type token as an ILSpy issue if the assembly is valid.","Reproduce on a single type to isolate it: decompiler.Decompile(typeHandle).","If the assembly is obfuscated, try deobfuscation or the IL disassembler (ReflectionDisassembler) instead."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    var tree = decompiler.Decompile(typeHandle);\n} catch (DecompilerException ex) {\n    logger.Error(ex, \"Failed to decompile {Entity}: {Inner}\", ex.DecompiledEntity?.FullName, ex.InnerException?.Message);\n    // continue with remaining types; the failure is scoped to this one entity\n}","preventionTips":["Decompile one entity at a time in a loop and isolate failures.","Keep assemblies and ILSpy versions current; many inner exceptions are fixed bugs."],"tags":["decompilation","error-wrapping","obfuscation","metadata"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}