{"record":{"id":"28fe9913252da4f3","repo":"dotnet/wpf","slug":"sr-onlysupportedoncollectionsanddictionaries","errorCode":null,"errorMessage":"SR.OnlySupportedOnCollectionsAndDictionaries","messagePattern":"SR\\.OnlySupportedOnCollectionsAndDictionaries","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs","lineNumber":221,"sourceCode":"            {\n                return null;\n            }\n\n            return _xamlType.GetEnumeratorMethod;\n        }\n\n        public virtual IEnumerator GetItems(object instance)\n        {\n            ArgumentNullException.ThrowIfNull(instance);\n            if (instance is IEnumerable enumerable)\n            {\n                return enumerable.GetEnumerator();\n            }\n\n            ThrowIfUnknown();\n            if (!_xamlType.IsCollection && !_xamlType.IsDictionary)\n            {\n                throw new NotSupportedException(SR.OnlySupportedOnCollectionsAndDictionaries);\n            }\n\n            MethodInfo getEnumMethod = GetEnumeratorMethod();\n            return (IEnumerator)getEnumMethod.Invoke(instance, Array.Empty<object>());\n        }\n\n        private bool IsPublic\n        {\n            get\n            {\n                if (_isPublic == ThreeValuedBool.NotSet)\n                {\n                    Type type = _xamlType.UnderlyingType.UnderlyingSystemType;\n                    _isPublic = type.IsVisible ? ThreeValuedBool.True : ThreeValuedBool.False;\n                }\n\n                return _isPublic == ThreeValuedBool.True;\n            }","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L203-L239","documentation":"XamlTypeInvoker.GetItems throws NotSupportedException('OnlySupportedOnCollectionsAndDictionaries') when the instance's XamlType is neither a collection nor a dictionary. GetItems enumerates the contents of XAML collection/dictionary types; the operation is undefined for other shapes, so it is rejected early. Instances that implement IEnumerable are handled by the fast path before this check.","triggerScenarios":"Calling XamlTypeInvoker.GetItems(instance) where instance is non-null, does not implement IEnumerable, the invoker is known, and _xamlType.IsCollection == false && _xamlType.IsDictionary == false — e.g. invoking it on a scalar or complex-object XamlType.","commonSituations":"Generic node-writer code that calls GetItems on every property value without checking the XamlType shape; schema changes where a type that used to be a collection becomes a plain object; mistakenly passing the containing object rather than the collection property value.","solutions":["Check xamlType.IsCollection || xamlType.IsDictionary before calling GetItems and skip/special-case other types.","Make the instance implement IEnumerable so the fast path returns its enumerator.","Catch NotSupportedException and treat the value as a single item rather than enumerable content."],"exampleFix":"// before\nvar e = invoker.GetItems(value);\n// after\nif (value is IEnumerable e0) { var e = e0.GetEnumerator(); } else if (xamlType.IsCollection || xamlType.IsDictionary) { var e = invoker.GetItems(value); }","handlingStrategy":"type-guard","validationCode":"bool enumerableOk = instance is IEnumerable || xamlType.IsCollection || xamlType.IsDictionary;","typeGuard":"static bool CanGetItems(object instance, XamlType t) => instance is IEnumerable || t.IsCollection || t.IsDictionary;","tryCatchPattern":"try { var e = invoker.GetItems(instance); } catch (NotSupportedException) { /* treat as single item */ }","preventionTips":["Gate GetItems behind IsCollection/IsDictionary checks.","Implement IEnumerable on content-bearing types.","Handle scalar property values separately in node-writer loops.","Cover all property value shapes with writer tests."],"tags":["xaml","notsupported","enumeration","api-misuse"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}