dotnet/wpf · error · ArgumentException

ArgumentException(SR.Format(SR.CollectionCannotContainNulls…

Error message

ArgumentException(SR.Format(SR.CollectionCannotContainNulls, "typeArguments"))

What it means

When a generic XamlType is constructed, its typeArguments list is validated: null entries are not permitted because each argument must itself be a XamlType. A null element throws ArgumentException with message CollectionCannotContainNulls('typeArguments').

Solutions

  1. Filter or throw on null entries before passing typeArguments
  2. Resolve every generic argument to a valid XamlType via SchemaContext.GetXamlType first
  3. Use a correctly sized list so no trailing null slots exist

Example fix

// before
var args = new XamlType[2];
args[0] = ctx.GetXamlType(typeof(int)); // args[1] stays null
var t = new XamlType("FuncOfInt", args, ctx); // throws
// after
var args = new List<XamlType> { ctx.GetXamlType(typeof(int)), ctx.GetXamlType(typeof(string)) };
var t = new XamlType("FuncOfInt", args, ctx);
Defensive patterns

Strategy: validation

Validate before calling

if (typeArguments is not null && typeArguments.Any(a => a is null)) throw new ArgumentException("typeArguments cannot contain null", nameof(typeArguments));

Type guard

bool allNonNull = typeArguments is null || typeArguments.All(a => a is not null);

Try / catch

try { var t = new XamlType(name, typeArguments, ctx); }
catch (ArgumentException ex) when (ex.Message.Contains("typeArguments")) { /* filter nulls and retry */ }

Prevention

When it happens

Trigger: Calling a XamlType constructor (e.g. the typeName/typeArguments form) or LookupTypeArguments path with an IList<XamlType> that contains a null element, such as new XamlType[] { someType, null }.

Common situations: Building generic type references dynamically where an argument failed to resolve to a XamlType and null was passed through; arrays preallocated to a size but not fully populated.

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/25c6a5d514854396. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlType.cs:1587

            {
                return new ReadOnlyCollection<T>(list);
            }

            return ReadOnlyCollection<T>.Empty;
        }

        private static ReadOnlyCollection<XamlType> GetTypeArguments(IList<XamlType> typeArguments)
        {
            if (typeArguments is null || typeArguments.Count == 0)
            {
                return null;
            }

            foreach (XamlType typeArg in typeArguments)
            {
                if (typeArg is null)
                {
                    throw new ArgumentException(SR.Format(SR.CollectionCannotContainNulls, "typeArguments"));
                }
            }

            return new List<XamlType>(typeArguments).AsReadOnly();
        }

        private static ReadOnlyCollection<XamlType> GetTypeArguments(Type type, XamlSchemaContext schemaContext)
        {
            Type genericType = type;
            while (genericType.IsArray)
            {
                genericType = genericType.GetElementType();
            }

            if (!genericType.IsGenericType)
            {
                return null;
            }

View on GitHub (pinned to 81131a70a4)