dotnet/wpf · error · ArgumentException

SR.CollectionCannotContainNulls

Error message

SR.CollectionCannotContainNulls

What it means

When resolving a generic XamlTypeName, each entry of xamlTypeName.TypeArguments must be non-null; a null type argument makes the generic type unresolvable, so ArgumentException with CollectionCannotContainNulls is thrown.

Solutions

  1. Filter or assert all TypeArguments are non-null before calling GetXamlType
  2. Resolve each type argument through GetXamlType first and handle null returns
  3. Log which index was null to find the producer of the bad argument

Example fix

// before
tn.TypeArguments.Add(null);
// after
foreach (var arg in args) if (arg != null) tn.TypeArguments.Add(arg);
Defensive patterns

Strategy: validation

Validate before calling

if (xamlTypeName.TypeArguments?.Contains(null) == true) throw new ArgumentException("TypeArguments must not contain null entries");

Type guard

bool HasValidTypeArgs(XamlTypeName t) => t?.TypeArguments is null || t.TypeArguments.All(a => a is not null);

Try / catch

try { var xt = ctx.GetXamlType(xtn); } catch (ArgumentException ex) when (ex.Message.Contains("TypeArguments")) { /* handle null generic argument */ }

Prevention

When it happens

Trigger: Calling GetXamlType on a XamlTypeName with TypeArguments populated including a null element (e.g. building the list by index and leaving a slot null).

Common situations: Programmatic construction of generic type names (List<T>, Dictionary<K,V> in XAML) where one argument failed to resolve to a XamlType beforehand.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs:295

            if (xamlTypeName.Name is null)
            {
                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, "xamlTypeName.Name"), nameof(xamlTypeName));
            }

            if (xamlTypeName.Namespace is null)
            {
                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, "xamlTypeName.Namespace"), nameof(xamlTypeName));
            }

            XamlType[] typeArgs = null;
            if (xamlTypeName.HasTypeArgs)
            {
                typeArgs = new XamlType[xamlTypeName.TypeArguments.Count];
                for (int i = 0; i < xamlTypeName.TypeArguments.Count; i++)
                {
                    if (xamlTypeName.TypeArguments[i] is null)
                    {
                        throw new ArgumentException(SR.Format(SR.CollectionCannotContainNulls, "xamlTypeName.TypeArguments"));
                    }

                    typeArgs[i] = GetXamlType(xamlTypeName.TypeArguments[i]);
                    if (typeArgs[i] is null)
                    {
                        return null;
                    }
                }
            }

            return GetXamlType(xamlTypeName.Namespace, xamlTypeName.Name, typeArgs);
        }

        protected internal virtual XamlType GetXamlType(string xamlNamespace, string name, params XamlType[] typeArguments)
        {
            ArgumentNullException.ThrowIfNull(xamlNamespace);
            ArgumentNullException.ThrowIfNull(name);
            if (typeArguments is not null)

View on GitHub (pinned to 81131a70a4)