dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(typeName))

Error message

ArgumentNullException(nameof(typeName))

What it means

The protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext) constructor requires a non-null type name string. It throws ArgumentNullException for typeName (and schemaContext) because a XamlType must always have a name to resolve against the schema context.

Solutions

  1. Ensure the derived class computes a valid non-null name before calling base(...)
  2. Throw a more descriptive exception or substitute a placeholder name in the derived constructor
  3. If the type cannot be named, use the UnknownType constructor path instead

Example fix

// before
: base(GetNameMaybe(), typeArguments, schemaContext) // GetNameMaybe() may return null
// after
string name = GetNameMaybe() ?? throw new InvalidOperationException("Type name unavailable");
: base(name, typeArguments, schemaContext)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeName is null) throw new InvalidOperationException("Derived XamlType requires a non-null name");

Type guard

bool ok = !string.IsNullOrEmpty(typeName) && schemaContext is not null;

Try / catch

try { var t = new MyXamlType(name, args, ctx); }
catch (ArgumentNullException ex) when (ex.ParamName == "typeName") { /* fix name derivation */ }

Prevention

When it happens

Trigger: A derived XamlType class passes a null typeName to this protected constructor, typically when the name comes from a lookup that failed and returned null.

Common situations: Custom XamlType subclass whose constructor derives typeName from reflection or schema data that is missing; refactoring changed the source of typeName to something nullable.

Related errors


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

Appendix: source

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

        private XamlSchemaContext _schemaContext;
        private readonly IList<XamlType> _typeArguments;

        // Thread safety: if setting outside ctor, do an interlocked compare against null
        private TypeReflector _reflector;

        /// <summary>
        /// Lazy init: NullableReference.IsSet is null when not initialized
        /// </summary>
        private NullableReference<Type> _underlyingType;

        // Lazy init: null until initialized
        // Thread safety: idempotent, assignment races are okay; do not assign incomplete values
        private ReadOnlyCollection<string> _namespaces;
        private ThreeValuedBool _isNameValid;

        protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)
        {
            _name = typeName ?? throw new ArgumentNullException(nameof(typeName));
            _schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));
            _typeArguments = GetTypeArguments(typeArguments);
        }

        public XamlType(string unknownTypeNamespace, string unknownTypeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)
        {
            ArgumentNullException.ThrowIfNull(unknownTypeNamespace);

            _name = unknownTypeName ?? throw new ArgumentNullException(nameof(unknownTypeName));
            _namespaces = new ReadOnlyCollection<string>(new string[] { unknownTypeNamespace });
            _schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));
            _typeArguments = GetTypeArguments(typeArguments);
            _reflector = TypeReflector.UnknownReflector;
        }

        public XamlType(Type underlyingType, XamlSchemaContext schemaContext)
            :this(underlyingType, schemaContext, null)
        {

View on GitHub (pinned to 81131a70a4)