{"record":{"id":"122ffd8c1d9001b4","repo":"unoplatform/uno","slug":"there-is-already-an-object-of-type-0-named-as","errorCode":null,"errorMessage":"There is already an object of type {0} named as '{1}'. Object names must be unique.","messagePattern":"There is already an object of type (.+?) named as '(.+?)'\\. Object names must be unique\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectNodeIterator.cs","lineNumber":181,"sourceCode":"\t\t\t\tforeach (var xn in GetNodes (null, new XamlObject (XamlLanguage.XData, obj)))\n\t\t\t\t\tyield return xn;\n\t\t\t} else {\n\t\t\t\t// Object - could become Reference\n\t\t\t\tvar val = xobj.GetRawValue ();\n\t\t\t\tif (!xobj.Type.IsContentValue (value_serializer_ctx) && val != null) {\n\t\t\t\t\tstring refName = NameResolver.GetName (val);\n\t\t\t\t\tif (refName != null) {\n\t\t\t\t\t\t// The target object is already retrieved, so we don't return the same object again.\n\t\t\t\t\t\tNameResolver.SaveAsReferenced (val); // Record it as named object.\n\t\t\t\t\t\t// Then return Reference object instead.\n\t\t\t\t\t\tforeach (var xn in GetNodes (null, new XamlObject (XamlLanguage.Reference, new Reference (refName))))\n\t\t\t\t\t\t\tyield return xn;\n\t\t\t\t\t\tyield break;\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// The object appeared in the xaml tree for the first time. So we store the reference with a unique name so that it could be referenced later.\n\t\t\t\t\t\trefName = GetReferenceName (xobj);\n\t\t\t\t\t\tif (NameResolver.IsCollectingReferences && NameResolver.Contains (refName))\n\t\t\t\t\t\t\tthrow new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, \"There is already an object of type {0} named as '{1}'. Object names must be unique.\", val.GetType (), refName));\n\t\t\t\t\t\tNameResolver.SetNamedObject (refName, val, true); // probably fullyInitialized is always true here.\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tyield return new XamlNodeInfo (XamlNodeType.StartObject, xobj);\n\t\t\t\t// If this object is referenced and there is no [RuntimeNameProperty] member, then return Name property in addition.\n\t\t\t\tif (val != null && xobj.Type.GetAliasedProperty (XamlLanguage.Name) == null) {\n\t\t\t\t\tstring name = NameResolver.GetReferencedName (val);\n\t\t\t\t\tif (name != null) {\n\t\t\t\t\t\tvar sobj = new XamlObject (XamlLanguage.String, name);\n\t\t\t\t\t\tforeach (var cn in GetMemberNodes (new XamlNodeMember (sobj, XamlLanguage.Name), new XamlNodeInfo [] { new XamlNodeInfo (name)}))\n\t\t\t\t\t\t\tyield return cn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tforeach (var xn in GetObjectMemberNodes (xobj))\n\t\t\t\t\tyield return xn;\n\t\t\t\tyield return new XamlNodeInfo (XamlNodeType.EndObject, xobj);\n\t\t\t}\n\t\t}","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectNodeIterator.cs#L163-L199","documentation":"Thrown by XamlObjectNodeIterator during XAML object-to-node conversion when two distinct objects in the object graph are assigned the same generated reference name (from GetReferenceName) while the NameResolver is collecting references. XAML requires object names to be unique within a namescope; a collision indicates the graph has duplicate or cyclic references that would produce ambiguous markup.","triggerScenarios":"Reading an object graph via XamlObjectReader where two different object instances hash/collide to the same reference name. This occurs when the iterator auto-generates reference names (because objects lack explicit x:Name) and two generate identically, or when the same named object appears twice.","commonSituations":"Serializing object graphs with shared/cyclic references where the reference-naming scheme collides. Custom objects without RuntimeNameProperty attributes being read by XamlObjectReader. Deep object graphs where the auto-generated name counter wraps or collides.","solutions":["Ensure objects that will be referenced have unique explicit names (via RuntimeNameProperty or by setting a unique name property).","Break cycles or eliminate duplicate references in the object graph before reading it.","If reading the same graph repeatedly, pass a fresh XamlObjectReader/NameResolver each time so reference names reset."],"exampleFix":"// before\nvar reader = new XamlObjectReader(graphWithDuplicateRefs, sctx);\nwhile (reader.Read()) { } // throws on duplicate name\n\n// after\n// give each shared object a unique name via [RuntimeNameProperty] tagged property\n// or remove the duplicate reference so each object appears once\nvar reader = new XamlObjectReader(deduplicatedGraph, sctx);","handlingStrategy":"try-catch","validationCode":"// Before reading, ensure shared objects have unique names\nforeach (var obj in graph) EnsureUniqueName(obj);\nvar reader = new XamlObjectReader(graph, sctx);","typeGuard":null,"tryCatchPattern":"try { while (reader.Read()) { } }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"unique\"))\n{ /* deduplicate the graph and retry with a new reader */ }","preventionTips":["Give referenced objects unique explicit names via RuntimeNameProperty.","Eliminate duplicate or cyclic references before serializing.","Use a fresh XamlObjectReader per graph read."],"tags":["xaml","invalid-operation","object-reader","naming","reference-graph","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}