dotnet/wpf · error · ArgumentNullException
ArgumentNullException(nameof(clrNamespace))
Error message
ArgumentNullException(nameof(clrNamespace))
What it means
XamlTypeMapper.AddMappingProcessingInstruction requires a non-null clrNamespace: the CLR namespace portion of the mapping (e.g. "System.Windows.Controls"). It throws ArgumentNullException when clrNamespace is null, because a mapping with no target CLR namespace is meaningless for type resolution.
Solutions
- Supply the CLR namespace string, e.g. mapper.AddMappingProcessingInstruction(ns, "MyCompany.MyApp.Controls", "MyAssembly").
- Parse the clr-namespace from the mapping URI and reject/handle mappings where it is missing before calling.
- Catch ArgumentNullException at the mapping-registration boundary and log which mapping entry was malformed.
Example fix
// before
mapper.AddMappingProcessingInstruction(xmlns, clrNsFromUri, assembly); // clrNsFromUri == null
// after
if (string.IsNullOrEmpty(clrNsFromUri))
throw new XamlParseException($"Mapping '{xmlns}' has no clr-namespace");
mapper.AddMappingProcessingInstruction(xmlns, clrNsFromUri, assembly); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(clrNamespace))
throw new ConfigurationErrorsException($"Mapping '{xmlNamespace}' is missing its clr-namespace"); Type guard
static bool TryGetClrNamespace(string mappingUri, out string clrNamespace)
{
clrNamespace = null;
const string prefix = "clr-namespace:";
if (mappingUri == null || !mappingUri.StartsWith(prefix)) return false;
int end = mappingUri.IndexOf(';');
clrNamespace = end < 0 ? mappingUri.Substring(prefix.Length) : mappingUri.Substring(prefix.Length, end - prefix.Length);
return clrNamespace.Length > 0;
} Try / catch
try
{
mapper.AddMappingProcessingInstruction(xmlNamespace, clrNamespace, assemblyName);
}
catch (ArgumentNullException ex) when (ex.ParamName == "clrNamespace")
{
throw new ConfigurationErrorsException($"Mapping '{xmlNamespace}' has no clr-namespace", ex);
} Prevention
- Parse 'clr-namespace:' URIs strictly and reject empty namespace segments
- Keep mapping metadata in a schema-validated config so clr-namespace is required
- Round-trip test: after AddMappingProcessingInstruction, GetType should resolve a known type
When it happens
Trigger: Calling mapper.AddMappingProcessingInstruction(xmlNamespace, null, assemblyName) — the clr-namespace portion is absent, e.g. parsing "clr-namespace:;assembly=X" or a mapping record built from a PI missing its clr-namespace part.
Common situations: Hand-edited XAML mapping processing instructions with empty clr-namespace, config-driven mapping tables missing the namespace column, or code generators emitting mappings with uninitialized namespace strings.
Related errors
- ArgumentNullException(nameof(assemblyName))
- ArgumentNullException(nameof(assemblyNames))
- ArgumentNullException(nameof(assemblyPath))
- ArgumentNullException(nameof(localName))
- ArgumentNullException(nameof(propertyName))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d93640827187281d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs:143
/// </param>
/// <param name="clrNamespace">
/// The "System.Windows.ComponentModel" argument in the mapping PI example.
/// </param>
/// <param name="assemblyName">
/// The "PresentationFramework" argument in the mapping PI example.
/// </param>
public void AddMappingProcessingInstruction(
string xmlNamespace,
string clrNamespace,
string assemblyName )
{
if( null == xmlNamespace )
{
throw new ArgumentNullException(nameof(xmlNamespace));
}
if( null == clrNamespace )
{
throw new ArgumentNullException(nameof(clrNamespace));
}
if( null == assemblyName )
{
throw new ArgumentNullException(nameof(assemblyName));
}
// Parameter validation : Check for String.Empty as well?
// Add mapping to the table keyed by xmlNamespace
ClrNamespaceAssemblyPair pair = new ClrNamespaceAssemblyPair(clrNamespace, assemblyName);
PITable[xmlNamespace] = pair;
// Add mapping to the table keyed by assembly and clrnamespace
string upperAssemblyName = assemblyName.ToUpper(
TypeConverterHelper.InvariantEnglishUS);
String fullName = $"{clrNamespace}#{upperAssemblyName}";
_piReverseTable[fullName] = xmlNamespace;View on GitHub (pinned to 81131a70a4)