{"record":{"id":"c2e4f83abe4d282a","repo":"unoplatform/uno","slug":"name-must-be-set-before-calling-tostring-method","errorCode":null,"errorMessage":"Name must be set before calling ToString method.","messagePattern":"Name 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":243,"sourceCode":"\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}\n\n\t\t\tif (TypeArguments.Count > 0)","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeName.cs#L225-L261","documentation":"Thrown by XamlTypeName.ToString(INamespacePrefixLookup) when the Name property is null. ToString needs both Namespace and Name to form a valid XAML type name string; a null Name produces a meaningless result, so the library throws InvalidOperationException.","triggerScenarios":"Calling ToString() on an XamlTypeName where Namespace is set but Name is still null. Typically from default constructor usage where only Namespace was assigned, or from new XamlTypeName(\"ns\", null).","commonSituations":"Incremental construction of XamlTypeName objects where Name is loaded from async or optional data that arrived as null. Copying name fields from a source object whose Name was unset.","solutions":["Ensure both Namespace and Name are non-null before calling ToString.","Prefer the XamlTypeName(xamlNamespace, name) constructor which makes both required at construction time.","Add a null check at the call site: if (!string.IsNullOrEmpty(tn.Name)) tn.ToString();"],"exampleFix":"// before\nvar tn = new XamlTypeName();\ntn.Namespace = \"http://example.com\";\n// Name never set\nreturn tn.ToString();\n\n// after\nvar tn = new XamlTypeName(\"http://example.com\", nameFromData ?? throw new InvalidOperationException(\"name required\"));\nreturn tn.ToString();","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(tn.Namespace) || string.IsNullOrEmpty(tn.Name))\n    throw new InvalidOperationException(\"Namespace and Name required\");\nvar s = tn.ToString();","typeGuard":"static bool IsComplete(XamlTypeName tn) => !string.IsNullOrEmpty(tn.Namespace) && !string.IsNullOrEmpty(tn.Name);","tryCatchPattern":"try { return tn.ToString(); }\ncatch (InvalidOperationException) { return fallbackName; }","preventionTips":["Use parameterized constructors that require both Namespace and Name.","Validate both properties before any ToString call on default-constructed instances."],"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"}