{"record":{"id":"cd4031193e27c861","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-typename","errorCode":null,"errorMessage":"ArgumentNullException(nameof(typeName))","messagePattern":"ArgumentNullException\\(nameof\\(typeName\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs","lineNumber":34,"sourceCode":"    [MarkupExtensionReturnType(typeof(Type))]\n    public class TypeExtension : MarkupExtension\n    {\n        private string _typeName;\n        private Type _type;\n\n        /// <summary>\n        /// Default Constructor\n        /// </summary>\n        public TypeExtension()\n        {\n        }\n\n        /// <summary>\n        /// Constructor that takes a type name\n        /// </summary>\n        public TypeExtension(string typeName)\n        {\n            _typeName = typeName ?? throw new ArgumentNullException(nameof(typeName));\n        }\n\n        /// <summary>\n        /// Constructor that takes a type\n        /// </summary>\n        public TypeExtension(Type type)\n        {\n            _type = type ?? throw new ArgumentNullException(nameof(type));\n        }\n\n        /// <summary>\n        /// Return an object that should be set on the targetObject's targetProperty\n        /// for this markup extension. TypeExtension resolves a string into a Type\n        /// and returns it.\n        /// </summary>\n        /// <param name=\"serviceProvider\">Object that can provide services for the markup extension.</param>\n        /// <returns>The object to set on this property.</returns>\n        public override object ProvideValue(IServiceProvider serviceProvider)","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs#L16-L52","documentation":"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.","triggerScenarios":"Calling new TypeExtension(null), or passing a variable that is null (e.g. an unresolved attribute value from XAML or config).","commonSituations":"Reading x:Type values from configuration that is missing; string variables from reflection that were never assigned; deserialization gaps leaving the type name null.","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."],"exampleFix":"// before\nvar ext = new TypeExtension(attributes[\"type\"]); // may be null\n// after\nvar ext = new TypeExtension(attributes[\"type\"] ?? throw new InvalidOperationException(\"x:Type name is required\"));","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(typeName)) throw new InvalidOperationException(\"TypeExtension requires a non-empty type name\");","typeGuard":"static bool IsValidTypeName(string? n) => !string.IsNullOrWhiteSpace(n);","tryCatchPattern":"try { var ext = new TypeExtension(typeName); } catch (ArgumentNullException) { /* handle missing type name from config/attributes */ }","preventionTips":["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."],"tags":["xaml","argumentnullexception","null-check","type-extension"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}