{"record":{"id":"bf4c103fd7816a79","repo":"dotnet/wpf","slug":"sr-objectreaderxamlnamepropertymustbestring","errorCode":null,"errorMessage":"SR.ObjectReaderXamlNamePropertyMustBeString","messagePattern":"SR\\.ObjectReaderXamlNamePropertyMustBeString","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":2143,"sourceCode":"                return false;\n            }\n\n            private static string ValidateNamePropertyAndFindName(MemberMarkupInfo propertyInfo)\n            {\n                // The name property needs to be a single value that is an atomic string. If it isn't, then it's not\n                // a valid name.\n                //\n                if (propertyInfo.Children.Count == 1)\n                {\n                    var valueInfo = propertyInfo.Children[0] as ValueMarkupInfo;\n                    if (valueInfo?.XamlNode.Value is string name)\n                    {\n                        return name;\n                    }\n                }\n\n                XamlMember property = propertyInfo.XamlNode.Member;\n                throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderXamlNamePropertyMustBeString, property.Name, property.DeclaringType));\n            }\n\n            // public override void Write(XamlWriter writer)\n            // {\n            //    ShouldWriteAsReference = ShouldWriteAsReference ?? false;\n            //    Write(writer, false);\n            // }\n\n            // internal void Write(XamlWriter writer, bool forceAsRecord)\n            // {\n            //    // See comment in ReferenceMarkupInfo.Write()\n            //    //\n            //    if (!forceAsRecord)\n            //    {\n            //        if (ShouldWriteAsReference.HasValue && ShouldWriteAsReference.Value)\n            //        {\n            //            var reference = new ReferenceMarkupInfo() { Target = this };\n            //            reference.Write(writer);","sourceCodeStart":2125,"sourceCodeEnd":2161,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L2125-L2161","documentation":"XamlObjectReader throws this when a member used as the x:Name-equivalent name property does not return a string. XAML names must be strings; if the designated name member's value is of another type, the reader cannot use it as an element name.","triggerScenarios":"new XamlObjectReader(obj) where the member identified as the name property (XamlType.Name comparison, e.g. via x:Uid/Name directive resolution) has a non-string value in the graph; GetXamlName-style helper reaches the fallback throw after failing to read a string name.","commonSituations":"Custom types whose Name property is typed as int/object instead of string; XamlType lookups where x:Name collides with a custom member named Name of a non-string type; data-bound or placeholder values left in the Name slot.","solutions":["Change the Name-like property to type string","Set the member's value to a string before constructing the reader","Use x:Name instead of a custom non-string member for naming","Override the XamlType's lookup of the Name member if the default resolution is wrong"],"exampleFix":"// before\npublic int Name { get; set; }\n// after\npublic string Name { get; set; }","handlingStrategy":"type-guard","validationCode":"if (nameMemberValue is not string s || string.IsNullOrEmpty(s)) throw new InvalidOperationException(\"Name member must be a non-empty string\");","typeGuard":"bool IsValidXamlName(object v) => v is string s && s.Length > 0;","tryCatchPattern":"try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains(\"NamePropertyMustBeString\")) { /* coerce or fix Name member type */ }","preventionTips":["Declare Name-like members as string","Use x:Name for naming instead of custom members","Coerce non-string name values before reading"],"tags":["xaml","naming","type-mismatch"],"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"}