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
- 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.
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
- 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.
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
- The type of collection does not have a parameterless…
- The type of dictionary does not have a parameterless…
- Unable to find property
- ' ' failed to create instance of type ' ', see inner…
- Unable to decode asset reference
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)