{"record":{"id":"a7d6c37406758e5c","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-typename-xamltype","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/Xaml/XamlType.cs","lineNumber":40,"sourceCode":"        private XamlSchemaContext _schemaContext;\n        private readonly IList<XamlType> _typeArguments;\n\n        // Thread safety: if setting outside ctor, do an interlocked compare against null\n        private TypeReflector _reflector;\n\n        /// <summary>\n        /// Lazy init: NullableReference.IsSet is null when not initialized\n        /// </summary>\n        private NullableReference<Type> _underlyingType;\n\n        // Lazy init: null until initialized\n        // Thread safety: idempotent, assignment races are okay; do not assign incomplete values\n        private ReadOnlyCollection<string> _namespaces;\n        private ThreeValuedBool _isNameValid;\n\n        protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)\n        {\n            _name = typeName ?? throw new ArgumentNullException(nameof(typeName));\n            _schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));\n            _typeArguments = GetTypeArguments(typeArguments);\n        }\n\n        public XamlType(string unknownTypeNamespace, string unknownTypeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)\n        {\n            ArgumentNullException.ThrowIfNull(unknownTypeNamespace);\n\n            _name = unknownTypeName ?? throw new ArgumentNullException(nameof(unknownTypeName));\n            _namespaces = new ReadOnlyCollection<string>(new string[] { unknownTypeNamespace });\n            _schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));\n            _typeArguments = GetTypeArguments(typeArguments);\n            _reflector = TypeReflector.UnknownReflector;\n        }\n\n        public XamlType(Type underlyingType, XamlSchemaContext schemaContext)\n            :this(underlyingType, schemaContext, null)\n        {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlType.cs#L22-L58","documentation":"The protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext) constructor requires a non-null type name string. It throws ArgumentNullException for typeName (and schemaContext) because a XamlType must always have a name to resolve against the schema context.","triggerScenarios":"A derived XamlType class passes a null typeName to this protected constructor, typically when the name comes from a lookup that failed and returned null.","commonSituations":"Custom XamlType subclass whose constructor derives typeName from reflection or schema data that is missing; refactoring changed the source of typeName to something nullable.","solutions":["Ensure the derived class computes a valid non-null name before calling base(...)","Throw a more descriptive exception or substitute a placeholder name in the derived constructor","If the type cannot be named, use the UnknownType constructor path instead"],"exampleFix":"// before\n: base(GetNameMaybe(), typeArguments, schemaContext) // GetNameMaybe() may return null\n// after\nstring name = GetNameMaybe() ?? throw new InvalidOperationException(\"Type name unavailable\");\n: base(name, typeArguments, schemaContext)","handlingStrategy":"type-guard","validationCode":"if (typeName is null) throw new InvalidOperationException(\"Derived XamlType requires a non-null name\");","typeGuard":"bool ok = !string.IsNullOrEmpty(typeName) && schemaContext is not null;","tryCatchPattern":"try { var t = new MyXamlType(name, args, ctx); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"typeName\") { /* fix name derivation */ }","preventionTips":["Ensure derived constructors always produce a name","Fail early in name-derivation helpers rather than passing null","Add Debug.Assert(name != null) in derived constructors"],"tags":["argumentnullexception","constructor","xamltype","xaml"],"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"}