stride3d/stride · error · YamlException
Cannot instantiate an object for type
Error message
Cannot instantiate an object for type [{objectContext.Descriptor}] What it means
ObjectSerializer.ReadMembers expects the object instance to have been created before reading its members. If objectContext.Instance is still null after the instantiation step, a YamlException with the node's span is thrown — the library could not construct an object of the target type.
Solutions
- Check for a prior exception/activation failure — the constructor or ObjectFactory likely threw or returned null
- Register realignment (abstract type -> concrete type) so the concrete type can be instantiated
- Ensure the type has an accessible parameterless or matching constructor
- Verify the YAML contains the concrete type tag when serializing polymorphic members
Example fix
// before
// property typed IFoo serialized as plain mapping, no concrete type
myFoo:
Bar: 1
// after
myFoo:
Bar: 1
~ConcreteFoo: {} // or ensure type tag / realignment is emitted Defensive patterns
Strategy: validation
Validate before calling
if (targetType.IsAbstract || targetType.IsInterface)
serializerContext.Services?.GetService<RealOffset>();
// ensure realignment registered:
if (!factory.CanInstantiate(targetType))
throw new InvalidOperationException($"Register a concrete type for {targetType}"); Type guard
static bool CanActivate(Type t) => !t.IsAbstract && !t.IsInterface && t.GetConstructor(Type.EmptyTypes) != null;
Try / catch
try
{
objectSerializer.ReadYaml(ref objectContext);
}
catch (YamlException ex) when (ex.Message.Contains("Cannot instantiate an object for type"))
{
// register realignment or supply an ObjectFactory, then retry
} Prevention
- Keep parameterless constructors on serialized types
- Register realignment for interface/abstract member types
- Ensure polymorphic YAML always carries the concrete type tag
- Watch for constructor exceptions logged before this error
When it happens
Trigger: Deserializing a mapping node whose target type cannot be instantiated: abstract type without a registered concrete mapping, type with no usable constructor, constructor throwing, or null produced by a custom ObjectFactory.
Common situations: Polymorphic YAML lacking the concrete type discriminator; constructor signature changed after refactor/upgrade; serializer for interface-typed members without realignment; activation errors in AOT/trimming environments.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- Cannot add item with key
- Cannot deserialize collection to readonly collection type
- Cannot deserialize collection to type
- Cannot set member [ . ]
- ex.Message
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/f3c375d99d3ed270.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Yaml/Serialization/Serializers/ObjectSerializer.cs:174
/// <summary>
/// Reads the members from the current stream.
/// </summary>
/// <typeparam name="TStart">The type of the t start.</typeparam>
/// <typeparam name="TEnd">The type of the t end.</typeparam>
/// <param name="objectContext"></param>
/// <returns>Return the object being read, by default thisObject passed by argument.</returns>
protected virtual void ReadMembers<TStart, TEnd>(ref ObjectContext objectContext)
where TStart : NodeEvent
where TEnd : ParsingEvent
{
var reader = objectContext.Reader;
var start = reader.Expect<TStart>();
// throws an exception while deserializing
if (objectContext.Instance == null)
{
throw new YamlException(start.Start, start.End, $"Cannot instantiate an object for type [{objectContext.Descriptor}]");
}
while (!reader.Accept<TEnd>())
{
ReadMember(ref objectContext);
}
reader.Expect<TEnd>();
}
/// <summary>
/// Reads an item of the object from the YAML flow (either a sequence item or mapping key/value item).
/// </summary>
/// <param name="objectContext"></param>
/// <exception cref="YamlException">Unable to deserialize property [{0}] not found in type [{1}].DoFormat(propertyName, typeDescriptor)</exception>
protected virtual void ReadMember(ref ObjectContext objectContext)
{
Scalar memberScalar;
string memberName;View on GitHub (pinned to 96fad776d2)