unoplatform/uno · error · InvalidOperationException
There is already an object of type {0} named as '{1}'. Objec
Error message
There is already an object of type {0} named as '{1}'. Object names must be unique. What it means
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.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectNodeIterator.cs:181
foreach (var xn in GetNodes (null, new XamlObject (XamlLanguage.XData, obj)))
yield return xn;
} else {
// Object - could become Reference
var val = xobj.GetRawValue ();
if (!xobj.Type.IsContentValue (value_serializer_ctx) && val != null) {
string refName = NameResolver.GetName (val);
if (refName != null) {
// The target object is already retrieved, so we don't return the same object again.
NameResolver.SaveAsReferenced (val); // Record it as named object.
// Then return Reference object instead.
foreach (var xn in GetNodes (null, new XamlObject (XamlLanguage.Reference, new Reference (refName))))
yield return xn;
yield break;
} else {
// 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.
refName = GetReferenceName (xobj);
if (NameResolver.IsCollectingReferences && NameResolver.Contains (refName))
throw 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));
NameResolver.SetNamedObject (refName, val, true); // probably fullyInitialized is always true here.
}
}
yield return new XamlNodeInfo (XamlNodeType.StartObject, xobj);
// If this object is referenced and there is no [RuntimeNameProperty] member, then return Name property in addition.
if (val != null && xobj.Type.GetAliasedProperty (XamlLanguage.Name) == null) {
string name = NameResolver.GetReferencedName (val);
if (name != null) {
var sobj = new XamlObject (XamlLanguage.String, name);
foreach (var cn in GetMemberNodes (new XamlNodeMember (sobj, XamlLanguage.Name), new XamlNodeInfo [] { new XamlNodeInfo (name)}))
yield return cn;
}
}
foreach (var xn in GetObjectMemberNodes (xobj))
yield return xn;
yield return new XamlNodeInfo (XamlNodeType.EndObject, xobj);
}
}View on GitHub (pinned to 0418340488)
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.
Example fix
// before
var reader = new XamlObjectReader(graphWithDuplicateRefs, sctx);
while (reader.Read()) { } // throws on duplicate name
// after
// give each shared object a unique name via [RuntimeNameProperty] tagged property
// or remove the duplicate reference so each object appears once
var reader = new XamlObjectReader(deduplicatedGraph, sctx); Defensive patterns
Strategy: try-catch
Validate before calling
// Before reading, ensure shared objects have unique names foreach (var obj in graph) EnsureUniqueName(obj); var reader = new XamlObjectReader(graph, sctx);
Try / catch
try { while (reader.Read()) { } }
catch (InvalidOperationException ex) when (ex.Message.Contains("unique"))
{ /* deduplicate the graph and retry with a new reader */ } Prevention
- Give referenced objects unique explicit names via RuntimeNameProperty.
- Eliminate duplicate or cyclic references before serializing.
- Use a fresh XamlObjectReader per graph read.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Namespace must be set before calling ToString method.
- Name must be set before calling ToString method.
- Thread has already started
- schemaContext
- instance type '{0}' must be public and non-nested.
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/122ffd8c1d9001b4.
Report an issue: GitHub.