{"record":{"id":"e02337eaf002e3e7","repo":"dotnet/wpf","slug":"sr-format-sr-textelementcollection-itemhasunexpectedtype","errorCode":null,"errorMessage":"SR.Format(SR.TextElementCollection_ItemHasUnexpectedType, \"range\", typeof(TextElementType).Name, typeof(TextElementType).Name)","messagePattern":"SR\\.Format\\(SR\\.TextElementCollection_ItemHasUnexpectedType, \"range\", typeof\\(TextElementType\\)\\.Name, typeof\\(TextElementType\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":335,"sourceCode":"\n            IEnumerator enumerator = range.GetEnumerator();\n            if (enumerator == null)\n            {\n                throw new ArgumentException(SR.TextElementCollection_NoEnumerator, nameof(range));\n            }\n\n            this.TextContainer.BeginChange();\n            try\n            {\n                while (enumerator.MoveNext())\n                {\n                    TextElementType element = enumerator.Current as TextElementType;\n\n                    if (element == null)\n                    {\n                        // REVIEW: It would be better design if we reviewed all elements in the range before starting an insert.\n                        // Otherwise, we might insert half the elements and then throw.\n                        throw new ArgumentException(SR.Format(SR.TextElementCollection_ItemHasUnexpectedType, \"range\", typeof(TextElementType).Name, typeof(TextElementType).Name), \"value\");\n                    }\n\n                    Add(element);\n                }\n            }\n            finally\n            {\n                this.TextContainer.EndChange();\n            }\n        }\n\n        //...................................................................\n        //\n        //  IEnumerable<TextElementType> Members\n        //\n        //...................................................................\n\n        #region IEnumerable<TextElementType> Members","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L317-L353","documentation":"Thrown by TextElementCollection<T>.AddRange when an item yielded by the range cannot be cast to the collection's element type TextElementType. Each enumerated element must be assignable to the collection's item type.","triggerScenarios":"Calling AddRange on a heterogeneous enumerable (e.g. ArrayList or non-generic IEnumerable) containing items that are not TextElementType instances; the cast 'as TextElementType' yields null.","commonSituations":"Passing mixed-type legacy collections; iterating a shared list that holds non-text objects; type changes after refactoring the element type parameter.","solutions":["Filter the source to only compatible items: range.OfType<TextElementType>() before AddRange.","Convert incompatible items into appropriate TextElement instances.","Use a strongly typed IEnumerable<TextElementType> source so mismatches surface at compile time."],"exampleFix":"// before\ncollection.AddRange(heterogeneousList);\n// after\ncollection.AddRange(heterogeneousList.OfType<TextElementType>());","handlingStrategy":"type-guard","validationCode":"var items = range as IEnumerable<TextElementType>\n    ?? range?.Cast<object>().Where(TextElementType_instance => TextElementType_instance is TextElementType).Cast<TextElementType>().ToList();\nif (items != null) collection.AddRange(items);","typeGuard":"static bool IsCompatible(object o) => o is TextElementType;\n// usage: collection.AddRange(heterogeneous.Where(IsCompatible).Cast<TextElementType>());","tryCatchPattern":"try { collection.AddRange(range); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"unexpected type\")) { /* fall back to filtered OfType<TextElementType>() insert */ }","preventionTips":["Use generic IEnumerable<TextElementType> sources wherever possible.","Filter with OfType<T>() when consuming non-generic or heterogeneous collections.","AddRange may partially insert before throwing — pre-validate all items first."],"tags":["wpf","type-mismatch","collection"],"backgroundTag":"type-mismatch","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"}