{"record":{"id":"e4792238e1b8db38","repo":"unoplatform/uno","slug":"namespace-must-be-set-before-calling-tostring-meth","errorCode":null,"errorMessage":"Namespace must be set before calling ToString method.","messagePattern":"Namespace must be set before calling ToString method\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeName.cs","lineNumber":241,"sourceCode":"\t\t\t\tvar l = new List<XamlTypeName> ();\n\t\t\t\tl.AddRange (typeArguments);\n\t\t\t\tTypeArguments = l;\n\t\t\t}\n\t\t}\n\n\t\tpublic string Name { get; set; }\n\t\tpublic string Namespace { get; set; }\n\t\tpublic IList<XamlTypeName> TypeArguments { get; private set; }\n\n\t\tpublic override string ToString ()\n\t\t{\n\t\t\treturn ToString (null);\n\t\t}\n\n\t\tpublic string ToString (INamespacePrefixLookup prefixLookup)\n\t\t{\n\t\t\tif (Namespace == null)\n\t\t\t\tthrow new InvalidOperationException (\"Namespace must be set before calling ToString method.\");\n\t\t\tif (Name == null)\n\t\t\t\tthrow new InvalidOperationException (\"Name must be set before calling ToString method.\");\n\n\t\t\tstring ret;\n\t\t\tif (prefixLookup == null)\n\t\t\t\tret = String.Concat (\"{\", Namespace, \"}\", Name);\n\t\t\telse {\n\t\t\t\tstring p = prefixLookup.LookupPrefix (Namespace);\n\t\t\t\tif (p == null)\n\t\t\t\t\tthrow new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, \"Could not lookup prefix for namespace '{0}'\", Namespace));\n\t\t\t\tret = p.Length == 0 ? Name : p + \":\" + Name;\n\t\t\t}\n\t\t\tstring arr = null;\n\t\t\tif (ret [ret.Length - 1] == ']') {\n\t\t\t\tint idx = ret.LastIndexOf ('[');\n\t\t\t\tarr = ret.Substring (idx);\n\t\t\t\tret = ret.Substring (0, idx);\n\t\t\t}","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeName.cs#L223-L259","documentation":"Thrown by XamlTypeName.ToString(INamespacePrefixLookup) when the Namespace property is null. ToString reconstructs a qualified XAML name string (e.g. {ns}Name) and cannot produce a valid result without a namespace, so it fails fast rather than silently emitting a malformed name.","triggerScenarios":"Calling ToString() (or the parameterized overload) on an XamlTypeName created via the default constructor XamlTypeName() where Namespace was never assigned. Or constructing with new XamlTypeName(null, \"Name\").","commonSituations":"Using the default constructor then setting only Name (forgetting Namespace). Debug/logging code that calls ToString() on a partially-constructed XamlTypeName. Deserialization or reflection code that builds XamlTypeName incrementally.","solutions":["Always set Namespace before calling ToString: var tn = new XamlTypeName { Name = \"Foo\", Namespace = \"http://schemas.example.com\" };","Use the two-or-three-argument constructor XamlTypeName(xamlNamespace, name) which sets Namespace explicitly.","Guard the call site: if (tn.Namespace != null) tn.ToString();"],"exampleFix":"// before\nvar tn = new XamlTypeName();\ntn.Name = \"Button\";\nConsole.WriteLine(tn.ToString());\n\n// after\nvar tn = new XamlTypeName(\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\", \"Button\");\nConsole.WriteLine(tn.ToString());","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(tn.Namespace)) throw new InvalidOperationException(\"Namespace required\");\nvar s = tn.ToString();","typeGuard":"static bool IsRenderable(XamlTypeName tn) => !string.IsNullOrEmpty(tn.Namespace);","tryCatchPattern":"try { return tn.ToString(prefixLookup); }\ncatch (InvalidOperationException) { return null; }","preventionTips":["Always use the XamlTypeName(namespace, name) constructor instead of default construction + property sets.","Centralize XamlTypeName creation in a factory that requires namespace."],"tags":["xaml","schema","invalid-operation","type-name","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}