icsharpcode/ILSpy · error · InvalidOperationException

Could not find type definition {fullTypeName} in type system

Error message

Could not find type definition {fullTypeName} in type system.

What it means

Thrown by DecompileType(FullTypeName) as InvalidOperationException when typeSystem.FindType(...).GetDefinition() returns null. The requested top-level type name is not present in the type system built from the assembly the CSharpDecompiler was constructed with.

Source

Thrown at ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs:1331

		/// Unlike Decompile(IMemberDefinition[]), this method will add namespace declarations around the type definitions.
		/// </remarks>
		public string DecompileTypesAsString(IEnumerable<TypeDefinitionHandle> types)
		{
			return SyntaxTreeToString(DecompileTypes(types));
		}

		/// <summary>
		/// Decompile the given type.
		/// </summary>
		/// <remarks>
		/// Unlike Decompile(IMemberDefinition[]), this method will add namespace declarations around the type definition.
		/// Note that decompiling types from modules other than the main module is not supported.
		/// </remarks>
		public SyntaxTree DecompileType(FullTypeName fullTypeName)
		{
			var type = typeSystem.FindType(fullTypeName.TopLevelTypeName).GetDefinition();
			if (type == null)
				throw new InvalidOperationException($"Could not find type definition {fullTypeName} in type system.");
			if (type.ParentModule != typeSystem.MainModule)
				throw new NotSupportedException($"Type {fullTypeName} was not found in the module being decompiled, but only in {type.ParentModule!.Name}");
			var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainModule);
			var namespaces = new HashSet<string>();
			syntaxTree = new SyntaxTree();
			RequiredNamespaceCollector.CollectNamespaces(type.MetadataToken, module, namespaces);
			var decompileRun = CreateDecompileRun(namespaces);
			DoDecompileTypes(new[] { (TypeDefinitionHandle)type.MetadataToken }, decompileRun, decompilationContext, syntaxTree);
			RunTransforms(syntaxTree, decompileRun, decompilationContext);
			return syntaxTree;
		}

		/// <summary>
		/// Decompile the given type.
		/// </summary>
		/// <remarks>
		/// Unlike Decompile(IMemberDefinition[]), this method will add namespace declarations around the type definition.
		/// </remarks>

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Resolve the name yourself first and check for null: decompiler.TypeSystem.FindType(name.TopLevelTypeName).GetDefinition().
  2. Confirm the assembly actually contains the type by enumerating module.TypeDefinitions and matching the full name.
  3. For nested types use the correct FullTypeName form (slash-separated nested names) and correct generic arity.
  4. If the type is in a dependency, load and decompile that dependency assembly instead.

Example fix

// before
var tree = decompiler.DecompileType(new FullTypeName("MyNamespace.MyType"));

// after
var name = new FullTypeName("MyNamespace.MyType");
var def = decompiler.TypeSystem.FindType(name.TopLevelTypeName).GetDefinition();
if (def == null)
    throw new InvalidOperationException($"'{name}' is not in {decompiler.TypeSystem.MainModule.AssemblyName}");
var tree = decompiler.DecompileType(name);
Defensive patterns

Strategy: validation

Validate before calling

var def = decompiler.TypeSystem.FindType(name.TopLevelTypeName).GetDefinition();
if (def == null)
    throw new InvalidOperationException($"'{name}' is not in {decompiler.TypeSystem.MainModule.AssemblyName}");
var tree = decompiler.DecompileType(name);

Prevention

When it happens

Trigger: Passing a FullTypeName for a type that lives in a referenced assembly (not the loaded one), a name with the wrong namespace or generic arity, an obfuscated/renamed type, or a name formatted incorrectly for FullTypeName (e.g. reflection FullName with '+' nested separators instead of '/').

Common situations: Asking for System.* types while decompiling an application assembly; version mismatch where the type moved namespaces; generic-arity mismatch; malformed nested-type name.

Related errors


AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13). Data as JSON: /api/errors/2ed49572ec8815bb. Report an issue: GitHub.