dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( nameof(element));

Error message

throw new ArgumentNullException( nameof(element));

What it means

XamlTypeMapper.GetTypeFromName(string typeName, DependencyObject element) throws ArgumentNullException when element is null. The element provides context for resolving type names in the XAML parser context (its context and assemblies), so a null element cannot be used for lookup.

Solutions

  1. Pass the DependencyObject currently being parsed as element
  2. If no element exists, use a type-resolution API that doesn't need element context (e.g. XamlTypeMapper.GetType or Type.GetType with assembly-qualified name)
  3. Guard the call site: only resolve names within an active element context

Example fix

// before
Type t = XamlTypeMapper.GetTypeFromName(name, null);
// after
Type t = string.IsNullOrEmpty(name)
    ? null
    : (element != null
        ? XamlTypeMapper.GetTypeFromName(name, element)
        : Type.GetType(name, throwOnError: false));
Defensive patterns

Strategy: type-guard

Validate before calling

if (element == null)
    return Type.GetType(typeName, throwOnError: false);
Type t = XamlTypeMapper.GetTypeFromName(typeName, element);

Type guard

DependencyObject RequireElement(DependencyObject element) =>
    element ?? throw new InvalidOperationException("Type-name resolution requires an active element context");

Try / catch

try
{
    var t = XamlTypeMapper.GetTypeFromName(typeName, element);
}
catch (ArgumentNullException ex) when (ex.ParamName == "element")
{
    log.LogWarning("No element context; falling back to Type.GetType for {Type}", typeName);
    var t = Type.GetType(typeName, false);
}

Prevention

When it happens

Trigger: Calling the static GetTypeFromName with a null DependencyObject — usually from custom parsing code that has no ambient element, or when element resolution earlier in the pipeline failed.

Common situations: Resolving type names referenced in XAML values outside an element tree; unit tests invoking the resolver without a real DependencyObject; scenarios where the root element wasn't yet created.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs:2155

        ///  located as a subelement or property on the passed element.
        /// </summary>
        /// <param name="typeName">
        ///   The full xaml name of a type, including an xml namespace prefix, if needed.
        ///   The name is of the form prefix:typename, such as MyNs:MyNewButton
        /// </param>
        /// <param name="element">
        ///   A DependencyObject that is logical parent for the type to be resolved.  This
        ///   is required because it is this element (or its ancestors) that contains
        ///   namespace mapping data that is needed to resolve the typeName.
        /// </param>
        /// <returns>
        ///  The resolved clr type.  Null if not found
        /// </returns>
        internal static Type GetTypeFromName(string typeName, DependencyObject element)
        {
            if (element == null)
            {
                throw new ArgumentNullException( nameof(element));
            }
            if (typeName == null)
            {
                throw new ArgumentNullException( nameof(typeName));
            }

            // Now map the prefix to an xml namespace uri
            int colonIndex = typeName.IndexOf(':');
            string prefix = string.Empty;
            if (colonIndex > 0)
            {
                prefix = typeName.Substring(0, colonIndex);
                typeName = typeName.Substring(colonIndex+1, typeName.Length-colonIndex-1);
            }

            // First, get the xmlns dictionary to map prefixes to xml namespace uris
            XmlnsDictionary prefixDictionary = element.GetValue(XmlAttributeProperties.XmlnsDictionaryProperty)
                                               as XmlnsDictionary;

View on GitHub (pinned to 81131a70a4)