{"record":{"id":"598a5ccbcc2993cf","repo":"stride3d/stride","slug":"the-type-of-dictionary-key-must-be-a-primary-type","errorCode":null,"errorMessage":"The type of dictionary key must be a primary type.","messagePattern":"The type of dictionary key must be a primary type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Quantum/DefaultNodeBuilder.cs","lineNumber":104,"sourceCode":"\n    /// <inheritdoc/>\n    public override void VisitCollection(IEnumerable collection, CollectionDescriptor descriptor)\n    {\n        if (!descriptor.HasIndexerAccessors)\n            throw new NotSupportedException(\"Collections that do not have indexer accessors are not supported in Quantum.\");\n\n        // Don't visit items unless they are primitive or enumerable (collections within collections)\n        if (IsCollection(descriptor.ElementType))\n        {\n            base.VisitCollection(collection, descriptor);\n        }\n    }\n\n    /// <inheritdoc/>\n    public override void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)\n    {\n        if (!PrimitiveTypeFilter.IsPrimitiveType(descriptor.KeyType))\n            throw new InvalidOperationException(\"The type of dictionary key must be a primary type.\");\n\n        // Don't visit items unless they are primitive or enumerable (collections within collections)\n        if (IsCollection(descriptor.ValueType))\n        {\n            base.VisitDictionary(dictionary, descriptor);\n        }\n    }\n\n    /// <inheritdoc/>\n    public override void VisitObjectMember(object container, ObjectDescriptor containerDescriptor, IMemberDescriptor member, object? value)\n    {\n        // If this member should contain a reference, create it now.\n        var containerNode = (IInitializingObjectNode)GetContextNode();\n        var guid = Guid.NewGuid();\n        var content = (MemberNode)NodeFactory.CreateMemberNode(this, guid, containerNode, member, value);\n        containerNode.AddMember(content);\n\n        PushContextNode(content);","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Quantum/DefaultNodeBuilder.cs#L86-L122","documentation":"DefaultNodeBuilder.VisitDictionary throws InvalidOperationException because Quantum represents dictionary entries through node indexes, which require key types the primitive filter can handle. Dictionary keys must be primitive types (string, numeric, enum-like primitives) to be addressable in the graph.","triggerScenarios":"Building a graph over a Dictionary<MyCustomClass, T> or any dictionary whose KeyType is not recognized by PrimitiveTypeFilter.IsPrimitiveType during VisitDictionary.","commonSituations":"Using enum-typed keys beyond the primitive filter, custom struct keys, or object keys in models observed by Quantum; config models with typed-key dictionaries.","solutions":["Change the dictionary key to a primitive type such as string or int","Convert complex keys to strings (e.g. serialize the key) before storing them in an observed dictionary","Exclude the dictionary from the node graph if complex keys are required","Catch InvalidOperationException and surface a clear model error to the developer"],"exampleFix":"// before\npublic Dictionary<MyKeyStruct, Item> Items = new Dictionary<MyKeyStruct, Item>();\n// after\npublic Dictionary<string, Item> Items = new Dictionary<string, Item>();","handlingStrategy":"validation","validationCode":"var d = TypeDescriptorFactory.Default.Find(dict.GetType()); if (d is DictionaryDescriptor dd && !PrimitiveTypeFilter.IsPrimitiveType(dd.KeyType)) throw new InvalidOperationException($\"Key type {dd.KeyType} is not primitive\");","typeGuard":null,"tryCatchPattern":"try { container.Build(root); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"dictionary key\")) { logger.LogError(ex, \"Non-primitive dictionary key in model\"); }","preventionTips":["Use string or int dictionary keys in Quantum-observed models","Serialize complex keys to strings before storing","Validate dictionary key types during model design review"],"tags":["quantum","dictionary","key-type"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}