dotnet/wpf · error · InvalidOperationException

InvalidOperationException(SR.XamlTypeNameNamespaceIsNull)

Error message

InvalidOperationException(SR.XamlTypeNameNamespaceIsNull)

What it means

ConvertToStringInternal cannot render a XamlTypeName to string form because its Namespace property is null. A XamlTypeName only round-trips to '{ns}Name' syntax when it carries a namespace; this library treats a namespace-less name as an invalid state rather than emitting a wrong string.

Solutions

  1. Set a valid Namespace on the XamlTypeName before calling ToString.
  2. Construct the instance with both namespace and name, e.g. new XamlTypeName(ns, name).
  3. If the namespace is only known as a prefix, resolve the prefix to the full namespace URI first.

Example fix

// before
var t = new XamlTypeName(null, "MyType");
var s = t.ToString(); // InvalidOperationException
// after
var t = new XamlTypeName("http://schemas.example.com/ns", "MyType");
var s = t.ToString(); // '{http://schemas.example.com/ns}MyType'
Defensive patterns

Strategy: type-guard

Validate before calling

if (xamlTypeName is null || xamlTypeName.Namespace is null)
    throw new InvalidOperationException("XamlTypeName must have a Namespace before ToString.");

Type guard

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

Try / catch

try { var s = xamlTypeName.ToString(); }
catch (InvalidOperationException) { /* reconstruct with namespace or skip */ }

Prevention

When it happens

Trigger: Calling ToString()/ToString(IList<XamlTypeName>, IXamlNamespaceResolver) on a XamlTypeName constructed without a namespace (e.g. new XamlTypeName(null, "Foo") or a default-initialized instance).

Common situations: Manually constructing XamlTypeName instances and forgetting the namespace argument; deserializing/parsing failures that left a partially populated instance; copying Name but not Namespace between instances.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

        internal static IList<XamlTypeName> ParseListInternal(string typeNameList, Func<string, string> prefixResolver, out string error)
        {
            GenericTypeNameParser nameParser = new GenericTypeNameParser(prefixResolver);
            IList<XamlTypeName> xamlTypeName = nameParser.ParseList(typeNameList, out error);
            return xamlTypeName;
        }

        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)
                {

View on GitHub (pinned to 81131a70a4)