dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( nameof(typeName));

Error message

throw new ArgumentNullException( nameof(typeName));

What it means

XamlTypeMapper.GetTypeFromName also throws ArgumentNullException when typeName is null. The method parses the name (optionally with an xmlns prefix before ':') and maps it to a CLR type; a null name has nothing to resolve.

Solutions

  1. Pass a non-null type name string
  2. Check the attribute/element value for null before calling the resolver
  3. Treat null as 'no type specified' and skip resolution at the call site

Example fix

// before
Type t = XamlTypeMapper.GetTypeFromName(attr.Value, element); // Value may be null
// after
if (!string.IsNullOrEmpty(attr.Value))
    Type t = XamlTypeMapper.GetTypeFromName(attr.Value, element);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(typeName))
    return null;
Type t = XamlTypeMapper.GetTypeFromName(typeName, element);

Type guard

bool HasTypeName(string typeName) => !string.IsNullOrWhiteSpace(typeName);

Try / catch

try
{
    var t = XamlTypeMapper.GetTypeFromName(typeName, element);
}
catch (ArgumentNullException ex) when (ex.ParamName == "typeName")
{
    log.LogWarning("Missing type name in XAML value; skipping resolution");
    t = null;
}

Prevention

When it happens

Trigger: Calling GetTypeFromName(null, element) — commonly when a XAML attribute value or element name was absent, or upstream parsing produced a null type string.

Common situations: Malformed XAML where a type reference attribute is missing; custom parser code forwarding element names without null checks; deserializing documents with empty type tags.

Related errors


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

Appendix: source

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

        ///   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;

            object xmlNamespaceObject = prefixDictionary?[prefix];

            // Then get the list of NamespaceMapEntry objects that maps the xml namespace uri to one

View on GitHub (pinned to 81131a70a4)