{"record":{"id":"4dc410ee54180a12","repo":"dotnet/efcore","slug":"can-t-translate-method-call-method-name-which","errorCode":null,"errorMessage":"Can't translate method '{call.Method.Name}' which has no declaring type","messagePattern":"Can't translate method '(.+?)' which has no declaring type","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1930,"sourceCode":"        }\n\n        Result =\n            ElementAccessExpression(Translate<ExpressionSyntax>(index.Object!))\n                .WithArgumentList(\n                    BracketedArgumentList(\n                        SingletonSeparatedList(\n                            Argument(\n                                Translate<ExpressionSyntax>(index.Arguments.Single())))));\n\n        return index;\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitMethodCall(MethodCallExpression call)\n    {\n        if (call.Method.DeclaringType is null)\n        {\n            throw new NotSupportedException($\"Can't translate method '{call.Method.Name}' which has no declaring type\");\n        }\n\n        using var _ = ChangeContext(ExpressionContext.Expression);\n\n        var arguments = TranslateMethodArguments(call.Method.GetParameters(), call.Arguments);\n\n        // For generic methods, we check whether the generic type arguments are inferrable (e.g. they all appear in the parameters), and\n        // only explicitly specify the arguments if not. Note that this isn't just for prettier code: anonymous types cannot be explicitly\n        // named in code.\n        SimpleNameSyntax methodIdentifier;\n        if (!call.Method.IsGenericMethod || GenericTypeParameterAreInferrable())\n        {\n            methodIdentifier = IdentifierName(call.Method.Name);\n        }\n        else\n        {\n            Check.DebugAssert(\n                call.Method.GetGenericArguments().All(ga => !ga.IsAnonymousType()),","sourceCodeStart":1912,"sourceCodeEnd":1948,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1912-L1948","documentation":"Thrown by LinqToCSharpSyntaxTranslator.VisitMethodCall when call.Method.DeclaringType is null. Reflection's MethodBase.DeclaringType is null only for global methods (and a few exotic cases); the translator needs a declaring type to render either a static receiver or an instance receiver expression, so a method with no declaring type cannot be translated.","triggerScenarios":"An expression tree invokes a method whose MethodInfo reports a null DeclaringType — global-scope methods from dynamic assemblies, certain DynamicMethod bindings, or hand-crafted DynamicMethod-based MethodInfos inserted into a tree.","commonSituations":"DynamicMethod or other reflection-emit scenarios where MethodInfo.DeclaringType is null; building expression trees around global functions; malformed MethodInfo wrappers from third-party IL emit libraries.","solutions":["Identify where the null-DeclaringType MethodInfo came from (DynamicMethod, reflection emit) and replace it with a normal MethodInfo on a real type.","Wrap the call so the target method is a static/instance method on a concrete type whose DeclaringType is non-null.","If the call truly is a global method, host it as a static method on a helper type and rewrite the tree to call that."],"exampleFix":"// before — DynamicMethod with no declaring type inserted into a tree\nDynamicMethod dm = new(\"Foo\", typeof(int), new[] { typeof(int) });\n// ... emit IL ...\nvar call = Expression.Call(dm.GetMethodInfo(), arg);\n\n// after — host the same logic as a static method on a real type\npublic static class Helpers { public static int Foo(int x) => /* ... */; }\nvar call = Expression.Call(\n    typeof(Helpers), nameof(Helpers.Foo), null, arg);","handlingStrategy":"validation","validationCode":"// Reject MethodCallExpressions whose method has no declaring type.\nusing System.Linq.Expressions;\nusing System.Reflection;\n\nbool HasNoDeclaringTypeCall(Expression e)\n{\n    bool bad = false;\n    new CallChecker(c => { if (c.Method.DeclaringType is null) bad = true; }).Visit(e);\n    return bad;\n}\n\nclass CallChecker(Action<MethodCallExpression> onCall) : ExpressionVisitor\n{\n    protected override Expression VisitMethodCall(MethodCallExpression c) { onCall(c); return base.VisitMethodCall(c); }\n}","typeGuard":"static bool MethodHasDeclaringType(MethodCallExpression c)\n    => c.Method.DeclaringType is not null;","tryCatchPattern":null,"preventionTips":["Avoid wrapping DynamicMethod or global-method MethodInfos inside expression trees.","Host any global method as a static method on a real type and call that.\n        Pre-scan trees for null-DeclaringType methods before translation."],"tags":["ef-core","expression-trees","method-calls","reflection","dynamic-method","code-generation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}