{"record":{"id":"1aa461ee0c5be6c9","repo":"stride3d/stride","slug":"failed-to-create-instance-of-type-type-wrong-factory","errorCode":null,"errorMessage":"Failed to create instance of type '{type}', wrong factory.","messagePattern":"Failed to create instance of type '(.+?)', wrong factory\\.","errorType":"exception","errorClass":"InstanceCreationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs","lineNumber":109,"sourceCode":"                else\n                {\n                    if (DefaultInterfaceImplementations.TryGetValue(type, out implementationType))\n                    {\n                        type = implementationType;\n                    }\n                }\n            }\n            return type;\n        }\n\n        /// <inheritdoc/>\n        public object Create(Type type)\n        {\n            type = GetDefaultImplementation(type);\n\n            // We can't instantiate primitives or arrays\n            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)\n                throw new InstanceCreationException($\"Failed to create instance of type '{type}', wrong factory.\");\n\n            if (type.GetConstructor(EmptyTypes) != null || type.IsValueType)\n            {\n                try\n                {\n                    return Activator.CreateInstance(type);\n                }\n                catch (Exception e)\n                {\n                    throw new InstanceCreationException($\"'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.\", e);\n                }\n            }\n\n            throw new InstanceCreationException($\"Failed to create instance of type '{type}', type does not have a parameterless constructor.\");\n        }\n\n        public class InstanceCreationException : Exception\n        {","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs#L91-L127","documentation":"DefaultObjectFactory.Create is asked to instantiate a type it cannot build: the type is a primitive or an array, so Activator/constructor creation is not applicable and it throws InstanceCreationException. The serializer's object graph expects a composite (class/struct) type at this node; the factory is the wrong tool for primitives and arrays, which are handled by their own type descriptors.","triggerScenarios":"Calling ObjectFactory/Create(Type) directly for a primitive (int, string, bool...) or array type; a serializer configuration (e.g. a custom IMappingFormatter or type registration) that routes a primitive/array to the default factory; deserializing a node whose declared type maps to an array but is processed as a regular object.","commonSituations":"Custom serialization code grabbing the default factory for all types; registering a service/scene type resolution where arrays of items (assets, entities) hit DefaultObjectFactory; refactors that changed a field type to an array without updating factory/descriptor registration.","solutions":["Don't call DefaultObjectFactory.Create for primitives or arrays — let the serializer dispatch through its type descriptors.","Register a dedicated descriptor/factory for the array or primitive type in your serializer configuration.","Check the type that reached Create: if you expected a class, fix the source type mapping (e.g. wrong type assigned in YamlSerializer settings).","Catch InstanceCreationException around factory usage and log the offending type to identify the misrouted path."],"exampleFix":"// before\nvar list = (int[]) objectFactory.Create(typeof(int[])); // throws\n// after\nvar list = new int[capacity]; // arrays are constructed by their descriptor, not the default factory","handlingStrategy":"try-catch","validationCode":"// C#\nbool canDefaultCreate(Type t) => !t.IsArray && !t.IsPrimitive && t != typeof(string);\nif (!canDefaultCreate(targetType))\n    throw new InvalidOperationException($\"{targetType} is a primitive/array; use the serializer's descriptor pipeline instead of DefaultObjectFactory.\");","typeGuard":"// C#\nstatic bool IsDefaultFactoryCompatible(Type t) =>\n    t != null && !t.IsArray && !t.IsPrimitive && t != typeof(string);","tryCatchPattern":"try { instance = objectFactory.Create(type); }\ncatch (InstanceCreationException ex) { throw new InvalidOperationException($\"Type {type} cannot be built by the default factory (primitive or array).\", ex); }","preventionTips":["Route all instantiation through the serializer's type descriptors, not Create() directly.","Check t.IsArray/t.IsPrimitive before invoking a generic factory.","When a field type changes to an array in a refactor, update any custom factory/descriptor registrations."],"tags":["yaml","serialization","factory","reflection"],"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-15T23:17:13.987Z"}