dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(unknownTypeName))

Error message

ArgumentNullException(nameof(unknownTypeName))

What it means

The public XamlType(unknownTypeNamespace, unknownTypeName, typeArguments, schemaContext) constructor creates a 'unknown type' placeholder for types that could not be resolved, and requires a non-null unknownTypeName. Null throws ArgumentNullException; the name plus namespace form the identity of the unknown type.

Solutions

  1. Supply the actual local type name string, even if the type is unresolved
  2. Use a descriptive placeholder name (e.g. the raw text from the XAML) when the true name is unavailable
  3. Validate the name argument before constructing

Example fix

// before
var t = new XamlType(ns, localName, null, schemaContext); // localName == null
// after
var t = new XamlType(ns, localName ?? "Unknown", null, schemaContext);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(unknownTypeName)) throw new ArgumentException("unknownTypeName required", nameof(unknownTypeName));

Type guard

bool ok = unknownTypeName is not null;

Try / catch

try { var t = new XamlType(ns, name, null, ctx); }
catch (ArgumentNullException ex) when (ex.ParamName == "unknownTypeName") { /* fall back to placeholder name */ }

Prevention

When it happens

Trigger: Calling this constructor with unknownTypeName == null, e.g. when propagating a name that failed to parse or was not supplied by the caller reporting the unresolved type.

Common situations: Code recording unresolved XAML namespace/type references where the local name was lost during parsing; interop layers forwarding null from external metadata.

Related errors


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

Appendix: source

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

        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)
        {
        }

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

        internal XamlType(string alias, Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker, TypeReflector reflector)
        {

View on GitHub (pinned to 81131a70a4)