icsharpcode/ILSpy · error · ArgumentException

Unsupported handle kind: {handle.Kind}

Error message

Unsupported handle kind: {handle.Kind}

What it means

ArgumentException thrown by GetIdString's default switch case when handle.Kind is not one of the definition kinds it can render: TypeDefinition, FieldDefinition, MethodDefinition, PropertyDefinition, EventDefinition. The C# doc-comment ID format is defined only for these definition handles; references and specs (TypeReference, TypeSpec, MethodSpec, MemberReference, GenericParameter, AssemblyReference, etc.) are rejected.

Source

Thrown at ICSharpCode.Decompiler/Documentation/IdStringProvider.cs:167

					break;

				case HandleKind.MethodDefinition:
					b.Append("M:");
					AppendMethodIdString(b, metadata, (MethodDefinitionHandle)handle, cppCliDialect);
					break;

				case HandleKind.PropertyDefinition:
					b.Append("P:");
					AppendPropertyIdString(b, metadata, (PropertyDefinitionHandle)handle, cppCliDialect);
					break;

				case HandleKind.EventDefinition:
					b.Append("E:");
					AppendEventIdString(b, metadata, (EventDefinitionHandle)handle);
					break;

				default:
					throw new ArgumentException($"Unsupported handle kind: {handle.Kind}", nameof(handle));
			}

			return b.ToString();
		}

		/// <summary>
		/// Gets the ID string (C# 4.0 spec, section A.3.1) for the specified entity.
		/// </summary>
		/// <remarks>
		/// The ID string is computed from the entity's metadata; for specialized members
		/// it describes the underlying member definition.
		/// </remarks>
		public static string GetIdString(this IEntity entity)
		{
			if (entity == null)
				throw new ArgumentNullException(nameof(entity));
			var module = entity.ParentModule?.MetadataFile;
			if (module == null || entity.MetadataToken.IsNil)

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Resolve the handle to its definition before computing the ID string (e.g. follow a MethodSpec to its MethodDefinition, a TypeSpec/TypeReference to its TypeDefinition).
  2. Guard on handle.Kind: only call GetIdString for HandleKind.TypeDefinition/FieldDefinition/MethodDefinition/PropertyDefinition/EventDefinition.
  3. Use GetIdString(this IEntity entity) on the resolved entity, which yields the underlying definition's ID.

Example fix

// before
var id = IdStringProvider.GetIdString(module, methodSpecHandle);

// after
var methodSpec = metadata.GetMethodSpecification(methodSpecHandle);
var defHandle = methodSpec.Method;
var id = IdStringProvider.GetIdString(module, defHandle);
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<HandleKind> IdKinds = new() {
    HandleKind.TypeDefinition, HandleKind.FieldDefinition,
    HandleKind.MethodDefinition, HandleKind.PropertyDefinition, HandleKind.EventDefinition };

if (!IdKinds.Contains(handle.Kind)) return null;
var id = IdStringProvider.GetIdString(module, handle);

Type guard

static bool HasIdStringHandle(EntityHandle h) => h.Kind is HandleKind.TypeDefinition or HandleKind.FieldDefinition or HandleKind.MethodDefinition or HandleKind.PropertyDefinition or HandleKind.EventDefinition;

Prevention

When it happens

Trigger: Passing a TypeReference, TypeSpec, MethodSpec, MemberReference, GenericParameter, or AssemblyReference handle to GetIdString(module, handle).

Common situations: Feeding a resolved-but-not-definition handle (e.g. a MethodSpec for an instantiated generic method, or a TypeReference from a signature) into GetIdString instead of resolving it to its definition handle first.

Related errors


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