stride3d/stride · error · InstanceCreationException
Failed to create instance of type
Error message
Failed to create instance of type '{type}', type does not have a parameterless constructor. What it means
DefaultObjectFactory.Create falls through to this error when the type has no accessible parameterless constructor, so Activator.CreateInstance cannot be used. The YAML/JSON deserializer needs to allocate instances of nodes' target types, and with no default constructor there is no way to do so without extra construction information.
Solutions
- Add a public parameterless constructor to the target type
- Register a custom IObjectFactory (or LambdaObjectFactory) that constructs the type with its required arguments
- Use the overload/flow that deserializes into an existing instance (e.g. DeserializeInto) instead of creating one
- Make the parameterless constructor public if it exists but is non-public
Example fix
// before
class Config { public Config(string name) { Name = name; } }
// after
class Config { public Config() { } public Config(string name) { Name = name; } } Defensive patterns
Strategy: type-guard
Validate before calling
if (type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null) == null)
throw new InvalidOperationException($"{type.Name} needs a public parameterless constructor for deserialization"); Type guard
static bool HasDefaultCtor(Type t) => t?.GetConstructor(Type.EmptyTypes) != null;
Try / catch
try { return serializer.Deserialize(stream, expectedType); }
catch (DefaultObjectFactory.InstanceCreationException ex) { throw new InvalidOperationException($"{expectedType} lacks a parameterless constructor", ex); } Prevention
- Add public parameterless constructors to all serialized types
- Register IObjectFactory implementations for immutable types
- Use DeserializeInto for types that only have parameterized constructors
- Add a unit test that instantiates every serializable type via Activator
When it happens
Trigger: Deserializing YAML/JSON into a class that only has parameterized constructors; a type whose parameterless constructor is private or internal; struct-less factory registration for a type with custom constructors.
Common situations: Immutable/config classes written with constructor injection only after a refactor; third-party POCO types lacking default ctors; types created via factory patterns where the deserializer was never told about the factory.
Related errors
- ' ' failed to create instance of type ' ', see inner…
- Unable to decode asset reference
- Unable to decode asset reference
- Unable to extract asset reference from object
- Unable to extract url reference from object
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/2f0d2ccc71211ae0.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs:123
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
{
public InstanceCreationException(string message) : base(message) { }
public InstanceCreationException(string message, Exception innerException) : base(message, innerException) { }
}
}
}
View on GitHub (pinned to 96fad776d2)