{"record":{"id":"c59aec6e1591b84f","repo":"dotnet/wpf","slug":"invalidoperationexception-sr-xamltypenamenameisnullorempty","errorCode":null,"errorMessage":"InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty)","messagePattern":"InvalidOperationException\\(SR\\.XamlTypeNameNameIsNullOrEmpty\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs","lineNumber":211,"sourceCode":"        }\n\n        internal string ConvertToStringInternal(Func<string, string> prefixGenerator)\n        {\n            StringBuilder result = new StringBuilder();\n            ConvertToStringInternal(result, prefixGenerator);\n            return result.ToString();\n        }\n\n        internal void ConvertToStringInternal(StringBuilder result, Func<string, string> prefixGenerator)\n        {\n            if (Namespace is null)\n            {\n                throw new InvalidOperationException(SR.XamlTypeNameNamespaceIsNull);\n            }\n\n            if (string.IsNullOrEmpty(Name))\n            {\n                throw new InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty);\n            }\n\n            if (prefixGenerator is null)\n            {\n                result.Append('{');\n                result.Append(Namespace);\n                result.Append('}');\n            }\n            else\n            {\n                string prefix = prefixGenerator.Invoke(Namespace);\n                if (prefix is null)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.XamlTypeNameCannotGetPrefix, Namespace));\n                }\n\n                if (prefix.Length != 0)\n                {","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs#L193-L229","documentation":"ConvertToStringInternal throws when the XamlTypeName's Name property is null or the empty string. A type name without a Name cannot be rendered, and the library treats this as an invalid object state instead of writing an empty element.","triggerScenarios":"Calling ToString()/ToString(IList<XamlTypeName>, ...) on a XamlTypeName whose Name is null or \"\" — e.g. default-constructed instance or one built with new XamlTypeName(ns, null).","commonSituations":"Default-initialized XamlTypeName fields that were never assigned; parse helpers that produced an empty instance; data-binding or reflection populating Name conditionally so it stays empty in some paths.","solutions":["Assign a non-empty Name before converting to string.","Guard the call with a check: if (string.IsNullOrEmpty(xamlTypeName.Name)) handle/skip.","Rebuild the instance from a trusted source (XamlTypeName.Parse of a valid string) instead of mutating fields."],"exampleFix":"// before\nvar t = new XamlTypeName(ns, \"\");\nvar s = t.ToString(); // InvalidOperationException\n// after\nvar t = new XamlTypeName(ns, \"MyType\");\nvar s = t.ToString();","handlingStrategy":"type-guard","validationCode":"if (string.IsNullOrEmpty(xamlTypeName?.Name))\n    throw new InvalidOperationException(\"XamlTypeName.Name must be non-empty before ToString.\");","typeGuard":"bool HasName(XamlTypeName t) => t is not null && !string.IsNullOrEmpty(t.Name);","tryCatchPattern":"try { var s = xamlTypeName.ToString(); }\ncatch (InvalidOperationException) { /* assign Name or skip instance */ }","preventionTips":["Initialize Name at construction; avoid parameterless defaults.","Add assertions in debug builds that Name is non-empty after parsing.","Guard batch serialization with a HasName filter."],"tags":["xaml","invalid-state","empty-value"],"backgroundTag":"empty-required-field","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"}