dotnet/wpf · error · ArgumentException
SR.CollectionCannotContainNulls
Error message
SR.CollectionCannotContainNulls
What it means
The XamlDirective constructor validates that the xamlNamespaces collection contains no null entries before storing it as a read-only list. A null namespace string would make directive resolution ambiguous, so ArgumentException(SR.CollectionCannotContainNulls, nameof(xamlNamespaces)) is thrown eagerly.
Solutions
- Sanitize the collection before constructing: remove or replace null entries with the correct namespace string.
- Use string.Empty for an empty namespace if that was the intent (e.g. CLR default namespace).
- Add a guard that throws with a clear message identifying which entry is null so callers can fix the source data.
Example fix
// before var directive = new XamlDirective(namespacesFromConfig, "Key", XamlLanguage.String, null, AllowedMemberLocations.Any); // namespacesFromConfig contains null // after var cleanNamespaces = namespacesFromConfig.Where(ns => ns is not null).ToList(); var directive = new XamlDirective(cleanNamespaces, "Key", XamlLanguage.String, null, AllowedMemberLocations.Any);
Defensive patterns
Strategy: validation
Validate before calling
if (xamlNamespaces is not null && xamlNamespaces.Any(ns => ns is null)) throw new ArgumentException("xamlNamespaces contains null entries"); Type guard
static bool HasNoNulls(IEnumerable<string?> src) => src is not null && !src.Any(ns => ns is null);
Try / catch
try { new XamlDirective(namespaces, name, type, invoker, loc); }
catch (ArgumentException ex) when (ex.ParamName == "xamlNamespaces") { /* sanitize list and retry */ } Prevention
- Coalesce null namespace strings to string.Empty at the data source
- Use string[] with nullable annotations disabled at boundaries
- Validate collections built from parsed/configured strings before passing on
When it happens
Trigger: Calling `new XamlDirective(xamlNamespaces, name, ...)` where the IEnumerable<string> passed as xamlNamespaces contains a null element, e.g. `new XamlDirective(new[] { null }, "MyDirective", ...)` or a list built with `list.Add(null)`.
Common situations: Programmatically building a namespace list where entries come from parsing or configuration and a missing namespace string yields null instead of empty; interop code converting non-.NET strings to XAML namespace strings.
Related errors
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
- Collection_BadRank
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e5463a467f4df1a8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlDirective.cs:44
}
#endif
_xamlNamespaces = immutableXamlNamespaces;
_allowedLocation = allowedLocation;
}
public XamlDirective(IEnumerable<string> xamlNamespaces, string name, XamlType xamlType,
XamlValueConverter<TypeConverter> typeConverter, AllowedMemberLocations allowedLocation)
: base(name, new MemberReflector(xamlType, typeConverter))
{
ArgumentNullException.ThrowIfNull(xamlType);
ArgumentNullException.ThrowIfNull(xamlNamespaces);
List<string> nsList = new List<string>(xamlNamespaces);
foreach (string ns in nsList)
{
if (ns is null)
{
throw new ArgumentException(SR.CollectionCannotContainNulls, nameof(xamlNamespaces));
}
}
_xamlNamespaces = nsList.AsReadOnly();
_allowedLocation = allowedLocation;
}
public XamlDirective(string xamlNamespace, string name)
:base(name, null)
{
ArgumentNullException.ThrowIfNull(xamlNamespace);
_xamlNamespaces = new ReadOnlyCollection<string>(new string[] { xamlNamespace });
_allowedLocation = AllowedMemberLocations.Any;
}
public AllowedMemberLocations AllowedLocation { get { return _allowedLocation; } }
View on GitHub (pinned to 81131a70a4)