{"record":{"id":"6513c10e8eb3114e","repo":"JamesNK/Newtonsoft.Json","slug":"newtonsoft-json-support-for-system-componentmodel","errorCode":null,"errorMessage":"Newtonsoft.Json support for System.ComponentModel is not compatible with trimming and has been disabled. Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported is set to false.","messagePattern":"Newtonsoft\\.Json support for System\\.ComponentModel is not compatible with trimming and has been disabled\\. Newtonsoft\\.Json\\.Linq\\.JToken\\.ComponentModelIsSupported is set to false\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":965,"sourceCode":"            foreach (JToken item in ChildrenTokens)\n            {\n                hashCode ^= item.GetDeepHashCode();\n            }\n            return hashCode;\n        }\n\n#if HAVE_COMPONENT_MODEL\n        string ITypedList.GetListName(PropertyDescriptor[]? listAccessors)\n        {\n            return string.Empty;\n        }\n\n        PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[]? listAccessors)\n        {\n#if HAVE_APPCONTEXT\n            if (!ComponentModelIsSupported)\n            {\n                throw new NotSupportedException(ComponentModelNotSupportedMessage);\n            }\n#endif\n            ICustomTypeDescriptor? d = First as ICustomTypeDescriptor;\n\n#pragma warning disable IL2026\n            return d?.GetProperties() ?? new PropertyDescriptorCollection(CollectionUtils.ArrayEmpty<PropertyDescriptor>());\n#pragma warning restore IL2026\n        }\n#endif\n\n        #region IList<JToken> Members\n        int IList<JToken>.IndexOf(JToken item)\n        {\n            return IndexOfItem(item);\n        }\n\n        void IList<JToken>.Insert(int index, JToken item)\n        {","sourceCodeStart":947,"sourceCodeEnd":983,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L947-L983","documentation":"Thrown by JContainer's ITypedList.GetItemProperties when the System.ComponentModel integration has been disabled at runtime via an AppContext feature switch. Newtonsoft.Json gates ComponentModel features (used by data-binding/PropertyGrid scenarios) behind the switch 'Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported' because they rely on reflection that trimming/AOT cannot guarantee. When the switch is explicitly set to false (or a host/SDK sets it), any call reaching the typed-list binding surface throws NotSupportedException.","triggerScenarios":"Calling ITypedList.GetItemProperties on a JContainer (JObject/JArray) — typically via a BindingSource, PropertyGrid, CurrencyManager, or any Windows Forms/WPF data-binding host that queries ITypedList — after the AppContext switch 'Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported' has been set to false. Occurs in trimmed/Native AOT apps or where a host forces the switch off.","commonSituations":"Publishing with .NET trimming or Native AOT (the SDK or a runtimeconfig flips ComponentModel off), then binding a JObject to a UI control; mixing Newtonsoft.Json.Linq nodes into a WinForms/WPF binding pipeline; upgrading to a Newtonsoft.Json version that introduced trimming switches.","solutions":["If you need ComponentModel data-binding, do not trim/AOT this path and leave the switch at its default (true) or set 'Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported' to true in runtimeconfig.","If you are trimming/AOT, stop using JToken nodes in ITypedList/data-binding UI; project to plain POCOs/DataTable before binding.","If a host forces the switch off, deserialize into typed CLR objects instead of JObject and bind those."],"exampleFix":"// before\nvar obj = JObject.Parse(json);\nbindingSource.DataSource = obj; // triggers ITypedList.GetItemProperties\n\n// after (trim/AOT-safe)\nvar model = JsonConvert.DeserializeObject<MyModel>(json);\nbindingSource.DataSource = model;","handlingStrategy":"validation","validationCode":"// Before binding a JContainer to a ComponentModel host, check the switch.\nbool componentModelSupported =\n    !AppContext.TryGetSwitch(\"Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported\", out bool enabled)\n    || enabled;\nif (!componentModelSupported)\n{\n    throw new InvalidOperationException(\n        \"ComponentModel binding is disabled; project JToken to a POCO before binding.\");\n}","typeGuard":null,"tryCatchPattern":"try { ((ITypedList)container).GetItemProperties(null); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"ComponentModelIsSupported\"))\n{\n    // ComponentModel disabled under trimming; fall back to POCO binding.\n}","preventionTips":["Avoid binding JObject/JArray directly to ComponentModel hosts; bind typed objects.","If you trim/AOT, do not reference the JToken data-binding surface at all so the linker can remove it.","Document the AppContext switch in your app's runtimeconfig when targeting trimming."],"tags":["newtonsoft-json","linq-to-json","componentmodel","trimming","aot","data-binding"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}