dotnet/wpf · error · XamlObjectReaderException

SR.ObjectReaderXamlNamePropertyMustBeString

Error message

SR.ObjectReaderXamlNamePropertyMustBeString

What it means

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.

Solutions

  1. Change the Name-like property to type string
  2. Set the member's value to a string before constructing the reader
  3. Use x:Name instead of a custom non-string member for naming
  4. Override the XamlType's lookup of the Name member if the default resolution is wrong

Example fix

// before
public int Name { get; set; }
// after
public string Name { get; set; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (nameMemberValue is not string s || string.IsNullOrEmpty(s)) throw new InvalidOperationException("Name member must be a non-empty string");

Type guard

bool IsValidXamlName(object v) => v is string s && s.Length > 0;

Try / catch

try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains("NamePropertyMustBeString")) { /* coerce or fix Name member type */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bf4c103fd7816a79. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs:2143

                return false;
            }

            private static string ValidateNamePropertyAndFindName(MemberMarkupInfo propertyInfo)
            {
                // The name property needs to be a single value that is an atomic string. If it isn't, then it's not
                // a valid name.
                //
                if (propertyInfo.Children.Count == 1)
                {
                    var valueInfo = propertyInfo.Children[0] as ValueMarkupInfo;
                    if (valueInfo?.XamlNode.Value is string name)
                    {
                        return name;
                    }
                }

                XamlMember property = propertyInfo.XamlNode.Member;
                throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderXamlNamePropertyMustBeString, property.Name, property.DeclaringType));
            }

            // public override void Write(XamlWriter writer)
            // {
            //    ShouldWriteAsReference = ShouldWriteAsReference ?? false;
            //    Write(writer, false);
            // }

            // internal void Write(XamlWriter writer, bool forceAsRecord)
            // {
            //    // See comment in ReferenceMarkupInfo.Write()
            //    //
            //    if (!forceAsRecord)
            //    {
            //        if (ShouldWriteAsReference.HasValue && ShouldWriteAsReference.Value)
            //        {
            //            var reference = new ReferenceMarkupInfo() { Target = this };
            //            reference.Write(writer);

View on GitHub (pinned to 81131a70a4)