{"record":{"id":"4429b5925b58f0bf","repo":"JamesNK/Newtonsoft.Json","slug":"newtonsoft-json-support-for-dynamic-is-not-compati-4429b5","errorCode":null,"errorMessage":"Newtonsoft.Json support for dynamic is not compatible with trimming and has been disabled. Newtonsoft.Json.Linq.JToken.DynamicIsSupported is set to false.","messagePattern":"Newtonsoft\\.Json support for dynamic is not compatible with trimming and has been disabled\\. Newtonsoft\\.Json\\.Linq\\.JToken\\.DynamicIsSupported is set to false\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":2498,"sourceCode":"        {\n            var p = new JPath(path);\n            return p.Evaluate(this, this, settings);\n        }\n\n#if HAVE_DYNAMIC\n        /// <summary>\n        /// Returns the <see cref=\"DynamicMetaObject\"/> responsible for binding operations performed on this object.\n        /// </summary>\n        /// <param name=\"parameter\">The expression tree representation of the runtime value.</param>\n        /// <returns>\n        /// The <see cref=\"DynamicMetaObject\"/> to bind this object.\n        /// </returns>\n        protected virtual DynamicMetaObject GetMetaObject(Expression parameter)\n        {\n#if HAVE_APPCONTEXT\n            if (!DynamicIsSupported)\n            {\n                throw new NotSupportedException(DynamicNotSupportedMessage);\n            }\n#endif\n#pragma warning disable IL2026, IL3050\n            return new DynamicProxyMetaObject<JToken>(parameter, this, new DynamicProxy<JToken>());\n#pragma warning restore IL2026, IL3050\n        }\n\n        /// <summary>\n        /// Returns the <see cref=\"DynamicMetaObject\"/> responsible for binding operations performed on this object.\n        /// </summary>\n        /// <param name=\"parameter\">The expression tree representation of the runtime value.</param>\n        /// <returns>\n        /// The <see cref=\"DynamicMetaObject\"/> to bind this object.\n        /// </returns>\n        DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)\n        {\n            return GetMetaObject(parameter);\n        }","sourceCodeStart":2480,"sourceCodeEnd":2516,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L2480-L2516","documentation":"Thrown by JToken.GetMetaObject (the IDynamicMetaObjectProvider entry point) when the runtime tries to bind dynamic operations (dynamic keyword, ExpandoObject-style member access) on a JToken but the feature switch Newtonsoft.Json.Linq.JToken.DynamicIsSupported has been set to false. This switch is disabled automatically by the .NET trimmer/AOT analyser because dynamic dispatch relies on reflection over types that trimming removes, so it is a deliberate trim-safety gate. The message names the switch so the user knows exactly what to flip.","triggerScenarios":"Declaring dynamic dyn = JObject.Parse(json); then dyn.someProperty, in an app published with PublishTrimmed=true, PublishAot=true, or with the AppContext switch Newtonsoft.Json.Linq.JToken.DynamicIsSupported=false set explicitly. Also surfaced when the trim analyser statically sees dynamic usage and the runtime guards trip.","commonSituations":"Migrating a desktop app to .NET 6+ Native AOT or a trimmed self-contained deployment. Publishing Blazor WASM or MAUI with trimming enabled. Setting the switch to false manually after seeing IL2026/IL3050 trim warnings. Using the dynamic keyword heavily with LINQ-to-JSON and then trimming.","solutions":["Stop using dynamic against JToken: cast to a strongly typed POCO via token.ToObject<T>() or use explicit indexing (token['prop']).","If dynamic is mandatory on a non-trimmed build, set the AppContext switch Newtonsoft.Json.Linq.JToken.DynamicIsSupported=true (and disable trimming/AOT for that assembly).","Disable PublishTrimmed / PublishSingleFile trimming for the process, or use a library build that excludes the dynamic guard."],"exampleFix":"// before\ndynamic dyn = JObject.Parse(json);\nConsole.WriteLine(dyn.user.name);\n\n// after (AOT/trim safe)\nvar doc = JObject.Parse(json);\nConsole.WriteLine(doc[\"user\"]?[\"name\"]);\n// or strongly typed\nvar user = doc.ToObject<User>();","handlingStrategy":"validation","validationCode":"// Avoid dynamic entirely on trimmed builds; index explicitly\nvar v = doc[\"prop\"]?.ToString();","typeGuard":"static bool IsDynamicSupported => AppContext.TryGetSwitch(\"Newtonsoft.Json.Linq.JToken.DynamicIsSupported\", out bool s) ? s : true;","tryCatchPattern":"try { dynamic d = token; var x = d.prop; } catch (NotSupportedException) { /* fall back to indexer */ }","preventionTips":["Do not use the dynamic keyword against JToken in trimmed/AOT apps.","Use ToObject<T>() or explicit indexers for AOT/trim safety.","Verify PublishTrimmed/PublishAot implications before adopting dynamic."],"tags":["dynamic","trimming","native-aot","notsupported","feature-switch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}