{"record":{"id":"3dfefb90b23a4914","repo":"stride3d/stride","slug":"the-given-container-does-not-match-the-expected-type-3dfefb","errorCode":null,"errorMessage":"The given container does not match the expected type.","messagePattern":"The given container does not match the expected type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Yaml/DictionaryWithIdsSerializer.cs","lineNumber":97,"sourceCode":"    }\n\n    /// <inheritdoc/>\n    protected override IDictionary CreatEmptyContainer(ITypeDescriptor descriptor)\n    {\n        var dictionaryDescriptor = (DictionaryDescriptor)descriptor;\n        var type = typeof(DictionaryWithItemIds<,>).MakeGenericType(dictionaryDescriptor.KeyType, dictionaryDescriptor.ValueType);\n        if (type.GetConstructor(Type.EmptyTypes) == null)\n            throw new InvalidOperationException(\"The type of dictionary does not have a parameterless constructor.\");\n        return (IDictionary)Activator.CreateInstance(type)!;\n    }\n\n    /// <inheritdoc/>\n    protected override void TransformAfterDeserialization(IDictionary container, ITypeDescriptor targetDescriptor, object targetCollection, ICollection<ItemId>? deletedItems = null)\n    {\n        var dictionaryDescriptor = (DictionaryDescriptor)targetDescriptor;\n        var type = typeof(DictionaryWithItemIds<,>).MakeGenericType(dictionaryDescriptor.KeyType, dictionaryDescriptor.ValueType);\n        if (!type.IsInstanceOfType(container))\n            throw new InvalidOperationException(\"The given container does not match the expected type.\");\n        var identifier = CollectionItemIdHelper.GetCollectionItemIds(targetCollection);\n        identifier.Clear();\n\n        // The collection may contain some initial data from its containing instance's ctor,\n        // let's replace the existing data with the data we have serialized\n        dictionaryDescriptor.Clear(targetCollection);\n\n        var enumerator = container.GetEnumerator();\n        while (enumerator.MoveNext())\n        {\n            var keyWithId = (IKeyWithId)enumerator.Key;\n            dictionaryDescriptor.AddToDictionary(targetCollection, keyWithId.Key, enumerator.Value);\n            identifier.Add(keyWithId.Key, keyWithId.Id);\n        }\n        if (deletedItems != null)\n        {\n            foreach (var deletedItem in deletedItems)\n            {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Yaml/DictionaryWithIdsSerializer.cs#L79-L115","documentation":"DictionaryWithIdsSerializer transforms a deserialized IDictionary container into a DictionaryWithItemIds<Key,Value> instance. This InvalidOperationException is thrown when the container object produced by deserialization is not actually an instance of the constructed DictionaryWithItemIds<,> generic type, meaning the deserializer's internal invariants were violated.","triggerScenarios":"Calling TransformObjectAfterRead during YAML asset deserialization when the IDictionary container handed to TransformAfterDeserialization was created by a different serializer or is a plain Dictionary/other IDictionary implementation instead of DictionaryWithItemIds<Key,Value> for the target descriptor's key/value types.","commonSituations":"Custom YAML serializers or hand-written deserialization pipelines feeding non-standard dictionary containers; a mismatch between the target descriptor's key/value types and the actual container type after a type or asset-schema version change; corrupted or hand-edited asset YAML producing unexpected collection types.","solutions":["Ensure the container passed to TransformAfterDeserialization is created via the DictionaryWithIdsSerializer/WithIds pipeline so it is a DictionaryWithItemIds<Key,Value>","Verify the target type descriptor's KeyType/ValueType match the container's generic arguments","Check that no custom ITypeDescriptor or serializer factory substitutes a plain dictionary for with-ids dictionaries","Regenerate the asset YAML with a compatible Stride version instead of hand-editing serialized collections"],"exampleFix":"// before (custom serializer yields plain dictionary)\nvar container = new Dictionary<string, MyItem>();\nserializer.TransformAfterDeserialization(container, descriptor, target);\n// after\nvar container = (IDictionary)Activator.CreateInstance(\n    typeof(DictionaryWithItemIds<,>).MakeGenericType(descriptor.KeyType, descriptor.ValueType));\nserializer.TransformAfterDeserialization(container, descriptor, target);","handlingStrategy":"validation","validationCode":"var expected = typeof(DictionaryWithItemIds<,>).MakeGenericType(descriptor.KeyType, descriptor.ValueType);\nif (!expected.IsInstanceOfType(container)) throw new InvalidOperationException(\"Container type mismatch before transform\");","typeGuard":"bool IsValidContainer(IDictionary c, DictionaryDescriptor d) =>\n    typeof(DictionaryWithItemIds<,>).MakeGenericType(d.KeyType, d.ValueType).IsInstanceOfType(c);","tryCatchPattern":"try { serializer.TransformAfterDeserialization(container, descriptor, target); }\ncatch (InvalidOperationException ex) { logger.Error(ex, \"Dictionary container type mismatch\"); throw; }","preventionTips":["Let the Stride serializer create dictionary containers; never substitute plain Dictionary","Keep key/value types consistent between descriptor and container","Avoid hand-editing serialized with-ids collections in YAML assets"],"tags":["yaml","deserialization","collections","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}