dotnet/wpf · error · ArgumentNullException
ArgumentNullException(nameof(typeName))
Error message
ArgumentNullException(nameof(typeName))
What it means
The TypeExtension(string) constructor stores the type name and immediately rejects null with ArgumentNullException, because a TypeExtension without a type name can never resolve to a Type. The name may still be validated later during ProvideValue, but null is rejected right here.
Solutions
- Pass a non-null type name string, e.g. new TypeExtension("local:MyClass").
- Guard at the call site: if (typeName != null) new TypeExtension(typeName).
- If the type is known at compile time, use new TypeExtension(typeof(MyClass)) instead.
- Fix the source of the null (config key, attribute parse) before construction.
Example fix
// before
var ext = new TypeExtension(attributes["type"]); // may be null
// after
var ext = new TypeExtension(attributes["type"] ?? throw new InvalidOperationException("x:Type name is required")); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(typeName)) throw new InvalidOperationException("TypeExtension requires a non-empty type name"); Type guard
static bool IsValidTypeName(string? n) => !string.IsNullOrWhiteSpace(n);
Try / catch
try { var ext = new TypeExtension(typeName); } catch (ArgumentNullException) { /* handle missing type name from config/attributes */ } Prevention
- Null-check attribute/config values before constructing TypeExtension.
- Prefer TypeExtension(typeof(T)) when the type is compile-time known.
- Validate XAML x:Type attribute presence during parsing.
When it happens
Trigger: Calling new TypeExtension(null), or passing a variable that is null (e.g. an unresolved attribute value from XAML or config).
Common situations: Reading x:Type values from configuration that is missing; string variables from reflection that were never assigned; deserialization gaps leaving the type name null.
Related errors
- ArgumentNullException(nameof(type))
- ArgumentNullException(nameof(value))
- XmlReader is null
- ArgumentNullException(nameof(schemaContext))
- ArgumentNullException(nameof(typeName))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/cd4031193e27c861.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs:34
[MarkupExtensionReturnType(typeof(Type))]
public class TypeExtension : MarkupExtension
{
private string _typeName;
private Type _type;
/// <summary>
/// Default Constructor
/// </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)View on GitHub (pinned to 81131a70a4)