{"record":{"id":"96e995fbe566b94e","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedparametertype-value-gettype-typeof-96e995","errorCode":null,"errorMessage":"SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(NamedElement))","messagePattern":"SR\\.Format\\(SR\\.UnexpectedParameterType, value\\.GetType\\(\\), typeof\\(NamedElement\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentStructures/SemanticBasicElement.cs","lineNumber":152,"sourceCode":"    /// </summary>\n    public class FigureStructure : SemanticBasicElement, IAddChild, IEnumerable<NamedElement>, IEnumerable\n    {\n        /// <summary>\n        ///\n        /// </summary>\n        public FigureStructure()\n        {\n            _elementType = FixedElement.ElementType.Figure;\n        }\n\n        void IAddChild.AddChild(object value)\n        {\n            if (value is NamedElement)\n            {\n                _elementList.Add((BlockElement)value);\n                return;\n            }\n            throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(NamedElement)), nameof(value));\n        }\n        \n        void IAddChild.AddText(string text) { }\n        \n        public void Add(NamedElement element)\n        {\n            ArgumentNullException.ThrowIfNull(element);\n            ((IAddChild) this).AddChild(element);\n        }\n        \n        IEnumerator<NamedElement> IEnumerable<NamedElement>.GetEnumerator()\n        {\n            throw new NotSupportedException();\n        }\n\n        IEnumerator IEnumerable.GetEnumerator()\n        {\n            return ((IEnumerable<NamedElement>)this).GetEnumerator();","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentStructures/SemanticBasicElement.cs#L134-L170","documentation":"This AddChild implementation (ListElement-style container) accepts only NamedElement children and throws ArgumentException with UnexpectedParameterType otherwise. The generic resource echoes the provided value's type plus the expected NamedElement type.","triggerScenarios":"Calling AddChild with a ListItemStructure or any non-NamedElement object on this container; XAML schemas where the parent expects only named leaf elements.","commonSituations":"Building ListStructure trees where the author confused the levels — adding ListItemStructure where only named marker/leaf elements belong, or vice versa.","solutions":["Add only NamedElement instances here.","Pass ListItemStructure children to ListStructure.AddChild instead.","Use the typed Add(NamedElement) API to get compile-time safety."],"exampleFix":"// before\nlistElement.AddChild(new ListItemStructure());\n// after\nparentListStructure.AddChild(new ListItemStructure());","handlingStrategy":"type-guard","validationCode":"if (value is not NamedElement) throw new ArgumentException($\"Expected NamedElement, got {value.GetType()}.\");","typeGuard":"bool IsNamedElement(object v) => v is NamedElement;","tryCatchPattern":"try { el.AddChild(value); } catch (ArgumentException) { /* add ListItems to ListStructure instead */ }","preventionTips":["Keep ListItemStructure under ListStructure, not under leaf elements.","Model the document tree against the documented structure schema.","Prefer typed Add methods so mismatches fail at compile time."],"tags":["wpf","document-structures","argument-validation"],"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"}