{"record":{"id":"7d1d394bdbfdce84","repo":"AvaloniaUI/Avalonia","slug":"invalid-method-handle","errorCode":null,"errorMessage":"Invalid method handle","messagePattern":"Invalid method handle","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/CompiledBindingPath.cs","lineNumber":275,"sourceCode":"        public IPropertyInfo Property { get; }\n\n        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;","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/CompiledBindingPath.cs#L257-L293","documentation":"Thrown as ArgumentException (paramName 'method') by the MethodAsDelegateElement constructor when MethodBase.GetMethodFromHandle(method) returns null or a non-MethodInfo. A RuntimeMethodHandle must correspond to a valid managed method; if the handle is invalid, zero, or points to a non-method metadata token, resolution fails.","triggerScenarios":"Constructing MethodAsDelegateElement with a default/uninitialized RuntimeMethodHandle, a handle obtained from a dynamic method whose metadata is gone, or a handle from an unloaded assembly. CompiledBindingPathBuilder.Method(handle, delegateType) forwards to this constructor.","commonSituations":"Source generator / compiled-binding pipeline passes a RuntimeMethodHandle for a method that was trimmed (AOT/trimming removed it), or a handle captured before the method was finalized. Passing default(RuntimeMethodHandle) by mistake. Assembly unload/reload invalidating a captured handle.","solutions":["Ensure the RuntimeMethodHandle passed to Method() is obtained from a live, loaded MethodInfo (e.g., methodInfo.MethodHandle) and not default.","In trimming/AOT scenarios, ensure the target method is preserved (DynamicallyAccessedMembers or root) so its handle resolves.","Validate the handle resolves before passing: MethodBase.GetMethodFromHandle(handle) != null."],"exampleFix":"// before\nbuilder.Method(default(RuntimeMethodHandle), delegateType); // throws\n// after\nbuilder.Method(myMethodInfo.MethodHandle, delegateType);","handlingStrategy":"validation","validationCode":"var methodInfo = MethodBase.GetMethodFromHandle(handle) as MethodInfo;\nif (methodInfo is null)\n    throw new ArgumentException(\"Invalid method handle\", nameof(handle));\nbuilder.Method(handle, delegateType);","typeGuard":"static bool IsValidMethodHandle(RuntimeMethodHandle h) =>\n    MethodBase.GetMethodFromHandle(h) is MethodInfo;","tryCatchPattern":"try { builder.Method(handle, delegateType); }\ncatch (ArgumentException ex) when (ex.ParamName == \"method\")\n{ /* re-acquire the handle from a live MethodInfo; check trimming roots */ }","preventionTips":["Obtain RuntimeMethodHandle from a live MethodInfo.MethodHandle, never default.","In trimming/AOT, preserve target methods with DynamicallyAccessedMembers.","Validate the handle resolves before passing it to the builder."],"tags":["compiled-binding","method-handle","reflection","trimming"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}