dotnet/wpf · error · InvalidOperationException

InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty)

Error message

InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty)

What it means

ConvertToStringInternal throws when the XamlTypeName's Name property is null or the empty string. A type name without a Name cannot be rendered, and the library treats this as an invalid object state instead of writing an empty element.

Solutions

  1. Assign a non-empty Name before converting to string.
  2. Guard the call with a check: if (string.IsNullOrEmpty(xamlTypeName.Name)) handle/skip.
  3. Rebuild the instance from a trusted source (XamlTypeName.Parse of a valid string) instead of mutating fields.

Example fix

// before
var t = new XamlTypeName(ns, "");
var s = t.ToString(); // InvalidOperationException
// after
var t = new XamlTypeName(ns, "MyType");
var s = t.ToString();
Defensive patterns

Strategy: type-guard

Validate before calling

if (string.IsNullOrEmpty(xamlTypeName?.Name))
    throw new InvalidOperationException("XamlTypeName.Name must be non-empty before ToString.");

Type guard

bool HasName(XamlTypeName t) => t is not null && !string.IsNullOrEmpty(t.Name);

Try / catch

try { var s = xamlTypeName.ToString(); }
catch (InvalidOperationException) { /* assign Name or skip instance */ }

Prevention

When it happens

Trigger: Calling ToString()/ToString(IList<XamlTypeName>, ...) on a XamlTypeName whose Name is null or "" — e.g. default-constructed instance or one built with new XamlTypeName(ns, null).

Common situations: Default-initialized XamlTypeName fields that were never assigned; parse helpers that produced an empty instance; data-binding or reflection populating Name conditionally so it stays empty in some paths.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs:211

        }

        internal string ConvertToStringInternal(Func<string, string> prefixGenerator)
        {
            StringBuilder result = new StringBuilder();
            ConvertToStringInternal(result, prefixGenerator);
            return result.ToString();
        }

        internal void ConvertToStringInternal(StringBuilder result, Func<string, string> prefixGenerator)
        {
            if (Namespace is null)
            {
                throw new InvalidOperationException(SR.XamlTypeNameNamespaceIsNull);
            }

            if (string.IsNullOrEmpty(Name))
            {
                throw new InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty);
            }

            if (prefixGenerator is null)
            {
                result.Append('{');
                result.Append(Namespace);
                result.Append('}');
            }
            else
            {
                string prefix = prefixGenerator.Invoke(Namespace);
                if (prefix is null)
                {
                    throw new InvalidOperationException(SR.Format(SR.XamlTypeNameCannotGetPrefix, Namespace));
                }

                if (prefix.Length != 0)
                {

View on GitHub (pinned to 81131a70a4)