{"record":{"id":"7a63a885f2a0e3e5","repo":"icsharpcode/ILSpy","slug":"cannot-get-rva-section-prefix-from-module","errorCode":null,"errorMessage":"Cannot get RVA section prefix from module","messagePattern":"Cannot get RVA section prefix from module","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs","lineNumber":1400,"sourceCode":"\t\t\t\tint rva = fieldDefinition.GetRelativeVirtualAddress();\r\n\t\t\t\tsectionPrefix = GetRVASectionPrefix(module, rva);\r\n\t\t\t\toutput.Write(\" at {1}_{0:X8}\", rva, sectionPrefix);\r\n\t\t\t}\r\n\r\n\t\t\tvar defaultValue = fieldDefinition.GetDefaultValue();\r\n\t\t\tif (!defaultValue.IsNil)\r\n\t\t\t{\r\n\t\t\t\toutput.Write(\" = \");\r\n\t\t\t\tWriteConstant(metadata, metadata.GetConstant(defaultValue));\r\n\t\t\t}\r\n\r\n\t\t\treturn sectionPrefix;\r\n\t\t}\r\n\r\n\t\tchar GetRVASectionPrefix(MetadataFile module, int rva)\r\n\t\t{\r\n\t\t\tif (module is not PEFile peFile)\r\n\t\t\t\tthrow new NotSupportedException(\"Cannot get RVA section prefix from module\");\r\n\t\t\tint sectionIndex = peFile.Reader.PEHeaders.GetContainingSectionIndex(rva);\r\n\t\t\tif (sectionIndex < 0)\r\n\t\t\t\treturn 'D';\r\n\t\t\tvar sectionHeader = peFile.Reader.PEHeaders.SectionHeaders[sectionIndex];\r\n\t\t\tswitch (sectionHeader.Name)\r\n\t\t\t{\r\n\t\t\t\tcase \".tls\":\r\n\t\t\t\t\treturn 'T';\r\n\t\t\t\tcase \".text\":\r\n\t\t\t\t\treturn 'I';\r\n\t\t\t\tdefault:\r\n\t\t\t\t\treturn 'D';\r\n\t\t\t}\r\n\t\t}\r\n\t\t#endregion\r\n\r\n\t\t#region Disassemble Property\r\n\t\tinternal static readonly EnumNameCollection<PropertyAttributes> propertyAttributes = new EnumNameCollection<PropertyAttributes>() {\r","sourceCodeStart":1382,"sourceCodeEnd":1418,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs#L1382-L1418","documentation":"NotSupportedException from ReflectionDisassembler.GetRVASectionPrefix when the module is not a PEFile. Computing the RVA section prefix (the I_/T_/D_ label for a field's '.field ... at <prefix>_<rva>') needs PE section headers, which only exist on a PEFile. A module that is a bare metadata source (no PE backing) cannot supply them.","triggerScenarios":"Disassembling a field that has HasFieldRVA on a MetadataFile that is not a PEFile, e.g. a module built from a MetadataReaderProvider created over a non-PE blob, or an in-memory/ephemeral module.","commonSituations":"Disassembling modules loaded via metadata-only paths without PE headers; CIL produced by assembly generators that do not emit a full PE image.","solutions":["Guard with 'if (module is PEFile)' before disassembling fields, or skip RVA-qualified fields for non-PE modules.","Provide a real PEFile (open from the on-disk assembly path) when you need RVA output.","Catch NotSupportedException and degrade the output by omitting the 'at <prefix>_<rva>' clause."],"exampleFix":"// before\ndisassembler.DisassembleField(module, fieldHandle); // throws when module is not PEFile and field has RVA\n\n// after\nif (module is PEFile)\n    disassembler.DisassembleField(module, fieldHandle);\nelse\n    logger.Warning(\"Skipping RVA field on non-PE module\");","handlingStrategy":"type-guard","validationCode":"if (module is PEFile peFile)\n    disassembler.DisassembleField(peFile, fieldHandle);\nelse\n    logger.Warning(\"Skipping RVA field on non-PE module {Module}\", module.FileName);","typeGuard":"static bool HasPeSectionHeaders(MetadataFile module) => module is PEFile;","tryCatchPattern":null,"preventionTips":["Open assemblies from their on-disk path (PEFile) when you need RVA-qualified IL output.","For metadata-only sources, skip fields with HasFieldRVA rather than disassembling them."],"tags":["disassembly","il","pe-file","metadata"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}