dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(schemaContext))

Error message

ArgumentNullException(nameof(schemaContext))

What it means

The protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext) constructor requires a non-null XamlSchemaContext. XamlType relies on the schema context for type lookup, namespace resolution, and settings such as SupportMarkupExtensionsWithDuplicateArity, so null is rejected with ArgumentNullException.

Solutions

  1. Create the XamlSchemaContext (e.g. new XamlSchemaContext() or an XamlXmlReaderSettings-derived context) before constructing the XamlType
  2. Pass an existing context instance from the reader/writer or schema provider
  3. Add constructor ordering so the context is initialized first

Example fix

// before
var myType = new MyXamlType("MyType", null, null); // schemaContext null
// after
var schemaContext = new XamlSchemaContext();
var myType = new MyXamlType("MyType", null, schemaContext);
Defensive patterns

Strategy: type-guard

Validate before calling

if (schemaContext is null) throw new InvalidOperationException("XamlSchemaContext required");

Type guard

bool ok = schemaContext is not null;

Try / catch

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

Prevention

When it happens

Trigger: Constructing a derived XamlType and passing null as schemaContext, e.g. because the context is created lazily after the type or a field holding it was never initialized.

Common situations: Custom schema/type provider code where the schema context field is uninitialized during early construction; unit tests constructing XamlType subclasses without a context; ordering issues in static initialization.

Related errors


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

Appendix: source

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

        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)