stride3d/stride · error · InstanceCreationException

Failed to create instance of type

Error message

Failed to create instance of type '{type}', wrong factory.

What it means

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.

Solutions

  1. Don't call DefaultObjectFactory.Create for primitives or arrays — let the serializer dispatch through its type descriptors.
  2. Register a dedicated descriptor/factory for the array or primitive type in your serializer configuration.
  3. Check the type that reached Create: if you expected a class, fix the source type mapping (e.g. wrong type assigned in YamlSerializer settings).
  4. Catch InstanceCreationException around factory usage and log the offending type to identify the misrouted path.

Example fix

// before
var list = (int[]) objectFactory.Create(typeof(int[])); // throws
// after
var list = new int[capacity]; // arrays are constructed by their descriptor, not the default factory
Defensive patterns

Strategy: try-catch

Validate before calling

// C#
bool canDefaultCreate(Type t) => !t.IsArray && !t.IsPrimitive && t != typeof(string);
if (!canDefaultCreate(targetType))
    throw new InvalidOperationException($"{targetType} is a primitive/array; use the serializer's descriptor pipeline instead of DefaultObjectFactory.");

Type guard

// C#
static bool IsDefaultFactoryCompatible(Type t) =>
    t != null && !t.IsArray && !t.IsPrimitive && t != typeof(string);

Try / catch

try { instance = objectFactory.Create(type); }
catch (InstanceCreationException ex) { throw new InvalidOperationException($"Type {type} cannot be built by the default factory (primitive or array).", ex); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/1aa461ee0c5be6c9. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs:109

                else
                {
                    if (DefaultInterfaceImplementations.TryGetValue(type, out implementationType))
                    {
                        type = implementationType;
                    }
                }
            }
            return type;
        }

        /// <inheritdoc/>
        public object Create(Type type)
        {
            type = GetDefaultImplementation(type);

            // We can't instantiate primitives or arrays
            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)
                throw new InstanceCreationException($"Failed to create instance of type '{type}', wrong factory.");

            if (type.GetConstructor(EmptyTypes) != null || type.IsValueType)
            {
                try
                {
                    return Activator.CreateInstance(type);
                }
                catch (Exception e)
                {
                    throw new InstanceCreationException($"'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.", e);
                }
            }

            throw new InstanceCreationException($"Failed to create instance of type '{type}', type does not have a parameterless constructor.");
        }

        public class InstanceCreationException : Exception
        {

View on GitHub (pinned to 96fad776d2)