icsharpcode/ILSpy · error · ArgumentException
definitions contains null element
Error message
definitions contains null element
What it means
ArgumentException from Decompile(IEnumerable<EntityHandle>) when the sequence contains a handle with IsNil == true (default(EntityHandle), token 0). Same guard as DecompileTypes but for the mixed entity (type/method/field/property/event) overload.
Source
Thrown at ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs:1375
/// </summary>
public SyntaxTree Decompile(params EntityHandle[] definitions)
{
return Decompile((IEnumerable<EntityHandle>)definitions);
}
/// <summary>
/// Decompile the specified types and/or members.
/// </summary>
public SyntaxTree Decompile(IEnumerable<EntityHandle> definitions)
{
if (definitions == null)
throw new ArgumentNullException(nameof(definitions));
syntaxTree = new SyntaxTree();
var namespaces = new HashSet<string>();
foreach (var entity in definitions)
{
if (entity.IsNil)
throw new ArgumentException("definitions contains null element");
RequiredNamespaceCollector.CollectNamespaces(entity, module, namespaces);
}
var decompileRun = CreateDecompileRun(namespaces);
bool first = true;
ITypeDefinition? parentTypeDef = null;
foreach (var entity in definitions)
{
switch (entity.Kind)
{
case HandleKind.TypeDefinition:
ITypeDefinition typeDef = module.GetDefinition((TypeDefinitionHandle)entity);
syntaxTree.Members.Add(DoDecompile(typeDef, decompileRun, new SimpleTypeResolveContext(typeDef)));
if (first)
{
parentTypeDef = typeDef.DeclaringTypeDefinition;
}
else if (parentTypeDef != null)
View on GitHub (pinned to 60c08fcb74)
Solutions
- Filter nil handles before the call: definitions.Where(h => !h.IsNil).
- Validate: if (definitions.Any(h => h.IsNil)) throw ... with caller context.
Example fix
// before var tree = decompiler.Decompile(handles); // after var valid = handles.Where(h => !h.IsNil).ToArray(); var tree = decompiler.Decompile(valid);
Defensive patterns
Strategy: validation
Validate before calling
var valid = definitions.Where(h => !h.IsNil).ToArray(); var tree = decompiler.Decompile(valid);
Type guard
static bool IsValidEntityHandle(EntityHandle h) => !h.IsNil;
Prevention
- Filter nil handles whenever you merge handle lists from several sources.
- Treat token 0 as a sentinel, never as a real entity.
When it happens
Trigger: A mixed EntityHandle collection built from multiple sources where one element is default(EntityHandle); merging handles without filtering the nil sentinel.
Common situations: Concatenating type + member handle lists where one lookup returned default; reading handles from an array with an empty slot.
Related errors
- types contains null element
- definitions
- handle
- Could not find type definition {fullTypeName} in type system
- HandleKind {handle.Kind} is not supported!
AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13).
Data as JSON: /api/errors/d81d399de479fd9d.
Report an issue: GitHub.