{"record":{"id":"c34712f5cd796540","repo":"stride3d/stride","slug":"cannot-deserialize-collection-to-type-thisobject-gettype-no","errorCode":null,"errorMessage":"Cannot deserialize collection to type [{thisObject.GetType()}]. No Add method found","messagePattern":"Cannot deserialize collection to type \\[(.+?)\\]\\. No Add method found","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/Serializers/CollectionSerializer.cs","lineNumber":144,"sourceCode":"                objectContext.Writer.Emit(new SequenceEndEventInfo(objectContext.Instance, objectContext.Instance.GetType()));\n            }\n        }\n\n        /// <summary>\n        /// Reads the collection items.\n        /// </summary>\n        /// <param name=\"objectContext\">The object context.</param>\n        /// <exception cref=\"System.InvalidOperationException\">Cannot deserialize list to type [{0}]. No Add method found.DoFormat(thisObject.GetType())\n        /// or\n        /// Cannot deserialize list to readonly collection type [{0}]..DoFormat(thisObject.GetType())</exception>\n        protected virtual void ReadCollectionItems(ref ObjectContext objectContext)\n        {\n            var collectionDescriptor = (CollectionDescriptor) objectContext.Descriptor;\n            var thisObject = objectContext.Instance;\n\n            if (!collectionDescriptor.HasAdd)\n            {\n                throw new InvalidOperationException($\"Cannot deserialize collection to type [{thisObject.GetType()}]. No Add method found\");\n            }\n            if (collectionDescriptor.IsReadOnly(thisObject))\n            {\n                throw new InvalidOperationException($\"Cannot deserialize collection to readonly collection type [{thisObject.GetType()}].\");\n            }\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            collectionDescriptor.Clear(thisObject);\n\n            var reader = objectContext.Reader;\n\n            var elementType = collectionDescriptor.ElementType;\n            var index = 0;\n            while (!reader.Accept<SequenceEnd>())\n            {\n                var currentDepth = objectContext.Reader.CurrentDepth;\n                var startParsingEvent = objectContext.Reader.Parser.Current;","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/Serializers/CollectionSerializer.cs#L126-L162","documentation":"CollectionSerializer needs the target collection type to expose an Add method to populate it during YAML deserialization. The type resolved for the collection node has no usable Add method, so deserialization cannot proceed and this InvalidOperationException is thrown.","triggerScenarios":"Deserializing a YAML mapping/sequence into a collection type whose CollectionDescriptor reports HasAdd == false — e.g. a custom collection with only AddRange, an immutable collection, or a type without a public Add.","commonSituations":"Switching a property type to a read-only/immutable collection (ImmutableList, IReadOnlyCollection wrappers); custom collection classes lacking a public Add; third-party collection types without Add.","solutions":["Give the collection type a public Add(item) method compatible with the descriptor","Deserialization target should be a mutable collection (List<T>, array, Dictionary, etc.)","Write a custom serializer for the type and register it with the SerializerFactory","Deserialize into a mutable type and expose the immutable one separately"],"exampleFix":"// before\npublic class MyCollection { public void AddRange(IEnumerable<int> items) { ... } }\n// after\npublic class MyCollection : IEnumerable<int>\n{\n    private readonly List<int> _items = new();\n    public void Add(int item) => _items.Add(item); // required\n    public IEnumerator<int> GetEnumerator() => _items.GetEnumerator();\n    ...\n}","handlingStrategy":"validation","validationCode":"var descriptor = TypeDescriptorFactory.Current.FindDescriptor(collectionType) as CollectionDescriptor;\nif (descriptor != null && !descriptor.HasAdd)\n    throw new InvalidOperationException($\"{collectionType} lacks an Add method; use a mutable collection\");","typeGuard":"static bool DeserializableAsCollection(Type t) =>\n    typeof(System.Collections.IEnumerable).IsAssignableFrom(t)\n    && t.GetMethod(\"Add\", BindingFlags.Public | BindingFlags.Instance) != null;","tryCatchPattern":"try\n{\n    serializer.ReadYaml(ref objectContext);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"No Add method found\"))\n{\n    // deserialize into List<T> or register a custom serializer\n}","preventionTips":["Use List<T>, arrays, Dictionary or other mutable collections in serialized models","Give custom collections a public Add method","Register custom serializers for immutable collection types"],"tags":["yaml","deserialization","collection"],"backgroundTag":"unsupported-operation","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"}