dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException(nameof(clrNamespace));

Error message

throw new ArgumentNullException(nameof(clrNamespace));

What it means

XamlTypeMapper's MappingUri/PageColorMapper constructor (the internal mapper entry type) requires xmlNamespace, assemblyName, and clrNamespace to be non-null; each missing value throws ArgumentNullException. This library validates constructor inputs up front so the mapper never holds partially initialized namespace-to-assembly mappings, which would corrupt XAML type resolution later.

Solutions

  1. Ensure clrNamespace is a non-empty CLR namespace string (e.g. 'System.Windows.Controls') before constructing the mapping
  2. If the namespace comes from reflection, verify the Type.Namespace value is not null (types in the global namespace return null)
  3. If the mapping is optional, skip adding it instead of constructing with null

Example fix

// before
var mapping = new MappingUri(xmlNamespace, assemblyName, type.Namespace); // type.Namespace can be null
// after
if (string.IsNullOrEmpty(type.Namespace)) throw new InvalidOperationException($"Type {type} has no namespace");
var mapping = new MappingUri(xmlNamespace, assemblyName, type.Namespace);
Defensive patterns

Strategy: validation

Validate before calling

if (xmlNamespace == null) throw new ArgumentException("xmlNamespace must not be null");
if (assemblyName == null) throw new ArgumentException("assemblyName must not be null");
if (clrNamespace == null) throw new ArgumentException("clrNamespace must not be null");
var mapping = new MappingUri(xmlNamespace, assemblyName, clrNamespace);

Type guard

static bool HasClrNamespace(Type t) => !string.IsNullOrEmpty(t.Namespace);

Try / catch

try { var m = new MappingUri(ns, asm, clrNs); }
catch (ArgumentNullException ex) { Log.Error(ex, "XamlTypeMapper mapping requires non-null arguments"); throw; }

Prevention

When it happens

Trigger: Calling the XamlTypeMapper MappingUri constructor (e.g. new MappingUri(xmlNamespace, assemblyName, clrNamespace)) with a null clrNamespace string — typically because the namespace string came from an uninitialized variable or a dictionary lookup that returned null.

Common situations: Hand-building a XamlTypeMapper programmatically (custom designer tooling, unit tests) where the clrNamespace was read from config or reflection metadata that was missing; refactors that renamed a constant leaving null in one of the three mapping arguments.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs:4236

        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="xmlNamespace">The XML NamespaceURi</param>
        /// <param name="assemblyName">Assembly to use when resolving a Tag</param>
        /// <param name="clrNamespace">Namespace within the assembly</param>
        public NamespaceMapEntry(string xmlNamespace,string assemblyName,string clrNamespace)
        {
            if (xmlNamespace == null)
                throw new ArgumentNullException(nameof(xmlNamespace));

            if (assemblyName == null)
                throw new ArgumentNullException(nameof(assemblyName));

            if (clrNamespace == null)
                throw new ArgumentNullException(nameof(clrNamespace));

            _xmlNamespace = xmlNamespace;
            _assemblyName = assemblyName;
            _clrNamespace = clrNamespace;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="xmlNamespace">The XML NamespaceURi</param>
        /// <param name="assemblyName">Assembly to use when resolving a Tag</param>
        /// <param name="clrNamespace">Namespace within the assembly</param>
        /// <param name="assemblyPath">Path to use when loading assembly.  This may be null.</param>
         internal NamespaceMapEntry(
                string xmlNamespace,
                string assemblyName,
                string clrNamespace,
                string assemblyPath) : this(xmlNamespace, assemblyName, clrNamespace)

View on GitHub (pinned to 81131a70a4)