{"record":{"id":"f6b868bcc41235ef","repo":"AvaloniaUI/Avalonia","slug":"unexpected-null-returned-from-type-gettypefromhand","errorCode":null,"errorMessage":"Unexpected null returned from Type.GetTypeFromHandle in MethodAsDelegateElement","messagePattern":"Unexpected null returned from Type\\.GetTypeFromHandle in MethodAsDelegateElement","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/CompiledBindingPath.cs","lineNumber":277,"sourceCode":"        public Func<WeakReference<object?>, IPropertyInfo, IPropertyAccessor> AccessorFactory { get; }\n\n        public bool AcceptsNull { get; }\n\n        public override string ToString()\n            => _isFirstElement ? Property.Name : $\".{Property.Name}\";\n    }\n\n    internal class MethodAsDelegateElement : ICompiledBindingPathElement\n    {\n        public MethodAsDelegateElement(\n            RuntimeMethodHandle method,\n            RuntimeTypeHandle delegateType,\n            bool acceptsNull)\n        {\n            Method = MethodBase.GetMethodFromHandle(method) as MethodInfo\n                ?? throw new ArgumentException(\"Invalid method handle\", nameof(method));\n            DelegateType = Type.GetTypeFromHandle(delegateType)\n                ?? throw new ArgumentException(\"Unexpected null returned from Type.GetTypeFromHandle in MethodAsDelegateElement\");\n            AcceptsNull = acceptsNull;\n        }\n\n        public MethodInfo Method { get; }\n\n        public Type DelegateType { get; }\n\n        public bool AcceptsNull { get; }\n    }\n\n    internal class MethodAsCommandElement : ICompiledBindingPathElement\n    {\n        public MethodAsCommandElement(string methodName, Action<object, object?> executeHelper, Func<object, object?, bool>? canExecuteHelper, string[] dependsOnElements)\n        {\n            MethodName = methodName;\n            ExecuteMethod = executeHelper;\n            CanExecuteMethod = canExecuteHelper;\n            DependsOnProperties = new HashSet<string>(dependsOnElements);","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/CompiledBindingPath.cs#L259-L295","documentation":"Thrown as ArgumentException by the MethodAsDelegateElement constructor when Type.GetTypeFromHandle(delegateType) returns null. A RuntimeTypeHandle must correspond to a loaded Type; a null result means the handle is invalid, zero, or refers to a type in an unloaded assembly.","triggerScenarios":"Constructing MethodAsDelegateElement with a default/uninitialized RuntimeTypeHandle, a handle to a type in an assembly that was unloaded, or a handle captured incorrectly. CompiledBindingPathBuilder.Method(handle, delegateType) forwards delegateType here.","commonSituations":"Passing default(RuntimeTypeHandle) instead of typeof(MyDelegate).TypeHandle. Type lives in an assembly not yet loaded or trimmed away (AOT/trimming). Reflection-based capture of a type handle before the assembly is resolved.","solutions":["Obtain the RuntimeTypeHandle from a live loaded Type via typeof(T).TypeHandle or type.TypeHandle, never default.","In trimming/AOT scenarios, ensure the delegate type is rooted/preserved so its handle resolves at runtime.","Validate Type.GetTypeFromHandle(delegateType) != null before passing to the builder."],"exampleFix":"// before\nbuilder.Method(methodHandle, default(RuntimeTypeHandle)); // throws\n// after\nbuilder.Method(methodHandle, typeof(Action<string>).TypeHandle);","handlingStrategy":"validation","validationCode":"var type = Type.GetTypeFromHandle(delegateType);\nif (type is null)\n    throw new ArgumentException(\"Invalid type handle\", nameof(delegateType));\nbuilder.Method(handle, delegateType);","typeGuard":"static bool IsValidTypeHandle(RuntimeTypeHandle h) =>\n    Type.GetTypeFromHandle(h) is not null;","tryCatchPattern":"try { builder.Method(handle, delegateType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"GetTypeFromHandle\"))\n{ /* obtain delegateType from typeof(T).TypeHandle; ensure assembly loaded */ }","preventionTips":["Obtain RuntimeTypeHandle from typeof(T).TypeHandle or a loaded Type.TypeHandle.","Never pass default(RuntimeTypeHandle).","In trimming/AOT, ensure the delegate type is rooted so its handle resolves."],"tags":["compiled-binding","type-handle","reflection","trimming"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}