dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(type))

Error message

ArgumentNullException(nameof(type))

What it means

The TypeExtension(Type) constructor throws ArgumentNullException when the type parameter is null. A markup TypeExtension must wrap a Type or a type name; constructing one with a null Type is an immediate argument error. (Likewise the string overload rejects a null typeName.)

Solutions

  1. Verify the Type lookup succeeded before constructing: check assembly.GetType result for null.
  2. Use typeof(KnownType) for compile-time-known types.
  3. Fix the assembly-qualified name / ensure the assembly is loaded so the lookup returns a real Type.

Example fix

// before
var ext = new TypeExtension(assembly.GetType(typeName)); // may be null
// after
var type = assembly.GetType(typeName) ?? throw new InvalidOperationException($"Type '{typeName}' not found");
var ext = new TypeExtension(type);
Defensive patterns

Strategy: validation

Validate before calling

var type = assembly.GetType(typeName);
if (type is null) throw new InvalidOperationException($"Type '{typeName}' could not be loaded from {assembly.FullName}");

Type guard

static bool IsValidType(Type? t) => t is not null;

Try / catch

try { var ext = new TypeExtension(resolvedType); } catch (ArgumentNullException) { /* type lookup failed — log and fallback */ }

Prevention

When it happens

Trigger: Calling new TypeExtension(null as Type), e.g. from reflection code where type = assembly.GetType(name) returned null or a generic lookup failed.

Common situations: assembly.GetType with a misspelled or version-drifted type name returning null; GetType on types not yet loaded; refactoring removed the type so the lookup yields null before the extension is built.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs:42

        /// </summary>
        public TypeExtension()
        {
        }

        /// <summary>
        /// Constructor that takes a type name
        /// </summary>
        public TypeExtension(string typeName)
        {
            _typeName = typeName ?? throw new ArgumentNullException(nameof(typeName));
        }

        /// <summary>
        /// Constructor that takes a type
        /// </summary>
        public TypeExtension(Type type)
        {
            _type = type ?? throw new ArgumentNullException(nameof(type));
        }

        /// <summary>
        /// Return an object that should be set on the targetObject's targetProperty
        /// for this markup extension. TypeExtension resolves a string into a Type
        /// and returns it.
        /// </summary>
        /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
        /// <returns>The object to set on this property.</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            // If a type was supplied, no context nor type name are needed
            if (_type is not null)
            {
                return _type;
            }

            // Validate the initialization.

View on GitHub (pinned to 81131a70a4)