dotnet/wpf · error · InvalidOperationException
InvalidOperationException(SR.Format(SR.XamlTypeNameCannotGet…
Error message
InvalidOperationException(SR.Format(SR.XamlTypeNameCannotGetPrefix, Namespace))
What it means
When converting a XamlTypeName to its '{prefix:Name}' form, the prefixGenerator (typically the namespace resolver's prefix lookup) returned null for the type's namespace, meaning no XML prefix is mapped to that namespace URI. The library throws because it cannot serialize the name without a prefix.
Solutions
- Register a prefix for the namespace URI in the resolver/prefix lookup before converting.
- Use the '{namespaceUri}Name' form via the resolver-free ToString overload that emits the full namespace.
- Verify the Namespace string exactly matches a declared xmlns URI (case and trailing slash sensitive).
Example fix
// before
var s = XamlTypeName.ToString(list, resolverWithoutPrefix); // InvalidOperationException
// after
nsResolver.AddNamespace("ex", "http://schemas.example.com/ns"); // ensure mapping exists
var s = XamlTypeName.ToString(list, nsResolver); Defensive patterns
Strategy: validation
Validate before calling
if (prefixGenerator?.Invoke(xamlTypeName.Namespace) is null)
throw new InvalidOperationException($"No prefix registered for namespace {xamlTypeName.Namespace}"); Type guard
bool HasPrefixFor(XamlTypeName t, Func<string,string> prefixGen) =>
t?.Namespace is not null && prefixGen?.Invoke(t.Namespace) is not null; Try / catch
try { var s = XamlTypeName.ToString(list, nsResolver); }
catch (InvalidOperationException ex) { /* register missing prefix or use '{uri}Name' form */ } Prevention
- Register all namespaces you will serialize with the resolver before conversion.
- Compare namespace URIs exactly (case, trailing slash) with declared xmlns values.
- Prefer the '{namespaceUri}Name' output form when no prefix mapping is guaranteed.
When it happens
Trigger: Calling XamlTypeName.ToString(...) with an IXamlNamespaceResolver that has no prefix registered for the type's Namespace URI; using a namespace-scoped resolver that does not cover the namespace in question.
Common situations: Namespace URI changed or typo'd so it no longer matches a registered xmlns; converting types from a namespace that was never declared in the resolver; serialization scope (NamespaceDeclaration context) where the prefix was only valid in a child scope.
Understand the failure class
Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.
Related errors
- ArgumentNullException(nameof(newNamespace))
- ArgumentNullException(nameof(oldNamespace))
- ArgumentNullException(nameof(writer))
- (no message - parameterless InvalidOperationException)
- SR.DuplicateXmlnsCompatAcrossAssemblies
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d9d33db56fa48182.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs:225
}
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)
{
result.Append(prefix);
result.Append(':');
}
}
if (HasTypeArgs)
{
// The subscript goes after the type args
ReadOnlySpan<char> name = GenericTypeNameScanner.StripSubscript(Name, out ReadOnlySpan<char> subscript);
result.Append(name);
result.Append('(');
ConvertListToStringInternal(result, TypeArguments, prefixGenerator);
result.Append(')');View on GitHub (pinned to 81131a70a4)