{"record":{"id":"7aa00c8dc2a2c7ed","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-element","errorCode":null,"errorMessage":"throw new ArgumentNullException( nameof(element));","messagePattern":"throw new ArgumentNullException\\( nameof\\(element\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs","lineNumber":2155,"sourceCode":"        ///  located as a subelement or property on the passed element.\n        /// </summary>\n        /// <param name=\"typeName\">\n        ///   The full xaml name of a type, including an xml namespace prefix, if needed.\n        ///   The name is of the form prefix:typename, such as MyNs:MyNewButton\n        /// </param>\n        /// <param name=\"element\">\n        ///   A DependencyObject that is logical parent for the type to be resolved.  This\n        ///   is required because it is this element (or its ancestors) that contains\n        ///   namespace mapping data that is needed to resolve the typeName.\n        /// </param>\n        /// <returns>\n        ///  The resolved clr type.  Null if not found\n        /// </returns>\n        internal static Type GetTypeFromName(string typeName, DependencyObject element)\n        {\n            if (element == null)\n            {\n                throw new ArgumentNullException( nameof(element));\n            }\n            if (typeName == null)\n            {\n                throw new ArgumentNullException( nameof(typeName));\n            }\n\n            // Now map the prefix to an xml namespace uri\n            int colonIndex = typeName.IndexOf(':');\n            string prefix = string.Empty;\n            if (colonIndex > 0)\n            {\n                prefix = typeName.Substring(0, colonIndex);\n                typeName = typeName.Substring(colonIndex+1, typeName.Length-colonIndex-1);\n            }\n\n            // First, get the xmlns dictionary to map prefixes to xml namespace uris\n            XmlnsDictionary prefixDictionary = element.GetValue(XmlAttributeProperties.XmlnsDictionaryProperty)\n                                               as XmlnsDictionary;","sourceCodeStart":2137,"sourceCodeEnd":2173,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs#L2137-L2173","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the DependencyObject currently being parsed as element","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)","Guard the call site: only resolve names within an active element context"],"exampleFix":"// before\nType t = XamlTypeMapper.GetTypeFromName(name, null);\n// after\nType t = string.IsNullOrEmpty(name)\n    ? null\n    : (element != null\n        ? XamlTypeMapper.GetTypeFromName(name, element)\n        : Type.GetType(name, throwOnError: false));","handlingStrategy":"type-guard","validationCode":"if (element == null)\n    return Type.GetType(typeName, throwOnError: false);\nType t = XamlTypeMapper.GetTypeFromName(typeName, element);","typeGuard":"DependencyObject RequireElement(DependencyObject element) =>\n    element ?? throw new InvalidOperationException(\"Type-name resolution requires an active element context\");","tryCatchPattern":"try\n{\n    var t = XamlTypeMapper.GetTypeFromName(typeName, element);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"element\")\n{\n    log.LogWarning(\"No element context; falling back to Type.GetType for {Type}\", typeName);\n    var t = Type.GetType(typeName, false);\n}","preventionTips":["Only call GetTypeFromName while an element is being parsed","Use Type.GetType with an assembly-qualified name when outside an element tree","Ensure root-element creation precedes value resolution in custom parsers"],"tags":["wpf","xaml","argument-null","type-resolution"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}