{"record":{"id":"674c63c80d633c3b","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-type","errorCode":null,"errorMessage":"ArgumentNullException(nameof(type))","messagePattern":"ArgumentNullException\\(nameof\\(type\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs","lineNumber":42,"sourceCode":"        /// </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)\n        {\n            // If a type was supplied, no context nor type name are needed\n            if (_type is not null)\n            {\n                return _type;\n            }\n\n            // Validate the initialization.","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs#L24-L60","documentation":"The TypeExtension(Type) constructor throws ArgumentNullException when the type parameter is null. A markup TypeExtension must wrap a Type or a type name; constructing one with a null Type is an immediate argument error. (Likewise the string overload rejects a null typeName.)","triggerScenarios":"Calling new TypeExtension(null as Type), e.g. from reflection code where type = assembly.GetType(name) returned null or a generic lookup failed.","commonSituations":"assembly.GetType with a misspelled or version-drifted type name returning null; GetType on types not yet loaded; refactoring removed the type so the lookup yields null before the extension is built.","solutions":["Verify the Type lookup succeeded before constructing: check assembly.GetType result for null.","Use typeof(KnownType) for compile-time-known types.","Fix the assembly-qualified name / ensure the assembly is loaded so the lookup returns a real Type."],"exampleFix":"// before\nvar ext = new TypeExtension(assembly.GetType(typeName)); // may be null\n// after\nvar type = assembly.GetType(typeName) ?? throw new InvalidOperationException($\"Type '{typeName}' not found\");\nvar ext = new TypeExtension(type);","handlingStrategy":"validation","validationCode":"var type = assembly.GetType(typeName);\nif (type is null) throw new InvalidOperationException($\"Type '{typeName}' could not be loaded from {assembly.FullName}\");","typeGuard":"static bool IsValidType(Type? t) => t is not null;","tryCatchPattern":"try { var ext = new TypeExtension(resolvedType); } catch (ArgumentNullException) { /* type lookup failed — log and fallback */ }","preventionTips":["Check assembly.GetType results for null before use.","Ensure the target assembly is loaded (referenced or Assembly.Load) before name lookups.","Use typeof(T) instead of name-based lookup when possible."],"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-21T21:30:21.729Z"}