{"record":{"id":"ec9422c234652e4e","repo":"dotnet/reactive","slug":"could-not-rewrite-method-with-name-0-without-a-declaringtype","errorCode":null,"errorMessage":"Could not rewrite method with name '{0}' without a DeclaringType.","messagePattern":"Could not rewrite method with name '(.+?)' without a DeclaringType\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/AsyncEnumerableRewriter.cs","lineNumber":89,"sourceCode":"            }\n\n            MethodInfo method;\n\n            //\n            // Find a corresponding method in the non-expression world, e.g. rewriting from\n            // the AsyncQueryable methods to the ones on AsyncEnumerable.\n            //\n            if (declType == typeof(AsyncQueryable))\n            {\n                method = FindEnumerableMethod(node.Method.Name, args, typeArgs);\n                args = FixupQuotedArgs(method, args);\n                return Expression.Call(obj, method, args);\n            }\n            else\n            {\n                if (declType == null)\n                {\n                    throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, \"Could not rewrite method with name '{0}' without a DeclaringType.\", node.Method.Name));\n                }\n\n                method = FindMethod(declType, node.Method.Name, args, typeArgs, BindingFlags.Static | (node.Method.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic));\n                args = FixupQuotedArgs(method, args);\n            }\n\n            return Expression.Call(obj, method, args);\n        }\n\n        protected override Expression VisitLambda<T>(Expression<T> node)\n        {\n            //\n            // Don't recurse into lambdas; all the ones returning IAsyncQueryable<T>\n            // are compatible with their IAsyncEnumerable<T> counterparts due to the\n            // covariant return type.\n            //\n            return node;\n        }","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/AsyncEnumerableRewriter.cs#L71-L107","documentation":"AsyncEnumerableRewriter.VisitMethodCall rewrites IQueryable method calls into their async IAsyncEnumerable equivalents. To find the replacement it needs the method's DeclaringType; when the node's method has no declaring type it throws NotSupportedException(\"Could not rewrite method with name '{0}' without a DeclaringType.\"). This is an internal invariant: queryable operators always come from a declaring type.","triggerScenarios":"An expression tree containing a MethodCall node whose Method.DeclaringType is null reaching the rewriter — typically from hand-built or reflection-emitted expressions, dynamic method handles, or trimmed/proxied methods.","commonSituations":"Constructing custom Expression.Call nodes with MethodInfo obtained via reflection on dynamic proxies, IL-emit scenarios, or expression manipulation libraries that strip declaring types.","solutions":["Ensure the expression's MethodInfo has a DeclaringType: resolve the concrete static method on its real class before composing the tree.","Avoid building Expression.Call with methods from dynamic/reflection-only sources in async queryables.","Handle the custom call before query execution — rewrite or replace it so the rewriter never sees it."],"exampleFix":"// before\nvar call = Expression.Call(dynMethod, arg); // dynMethod.DeclaringType == null\nvar q = source.Provider.CreateQuery<int>(call);\n// after\nvar concrete = typeof(KnownOps).GetMethod(nameof(KnownOps.MyOp));\nvar call = Expression.Call(concrete, arg);\nvar q = source.Provider.CreateQuery<int>(call);","handlingStrategy":"validation","validationCode":"if (node.Method.DeclaringType == null) throw new InvalidOperationException($\"Method '{node.Method.Name}' lacks a DeclaringType and cannot be rewritten\");","typeGuard":"bool IsRewritable(MethodCallExpression n) => n.Method.DeclaringType is not null;","tryCatchPattern":"try { var rewritten = rewriter.Visit(node); } catch (NotSupportedException ex) when (ex.Message.Contains(\"without a DeclaringType\")) { // execute client-side instead result = source.AsEnumerable().Select(...); }","preventionTips":["Build expressions only with MethodInfo resolved from concrete types, not dynamic/reflection-only handles.","Avoid proxies and IL-emit inside async queryable expression trees.","Rewrite exotic calls client-side (AsEnumerable) before query execution."],"tags":["expression-translation","reflection","async"],"backgroundTag":"internal-invariant-violation","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}