stride3d/stride · error · InstanceCreationException

'{typeof(Activator)}' failed to create instance of type '{ty

Error message

'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.

What it means

DefaultObjectFactory.Create uses Activator.CreateInstance(type) when the type has a parameterless constructor; if the runtime throws while constructing (e.g. the constructor itself threw, or the type is generic/open or abstract), it wraps the exception in InstanceCreationException. This is the YAML deserializer's way of reporting that object instantiation failed mid-construction, keeping the original exception as InnerException.

Solutions

  1. Read the InnerException to find the actual failure inside the type's constructor and fix that cause
  2. Ensure the deserialized type is a closed, concrete, instantiable type (not abstract, interface, or open generic)
  3. Register a custom IObjectFactory that knows how to construct the problematic type instead of relying on the parameterless constructor
  4. Give the type a public parameterless constructor that does not perform environment-dependent side effects

Example fix

// before
var entity = serializer.Deserialize(stream, typeof(PluginBase)); // abstract -> throws
// after
var entity = serializer.Deserialize(stream, typeof(AudioPlugin)); // concrete closed type
Defensive patterns

Strategy: try-catch

Validate before calling

if (type == null || type.IsAbstract || type.IsInterface || type.IsGenericTypeDefinition || type.GetConstructor(Type.EmptyTypes) == null)
    throw new InvalidOperationException($"Type {type} cannot be instantiated via parameterless ctor");

Type guard

static bool IsInstantiable(Type t) => t != null && !t.IsAbstract && !t.IsInterface && !t.IsGenericTypeDefinition && t.GetConstructor(Type.EmptyTypes) != null;

Try / catch

try { var obj = serializer.Deserialize(stream, type); }
catch (DefaultObjectFactory.InstanceCreationException ex) { log(ex.InnerException); throw new LoadException($"Cannot create {type}", ex); }

Prevention

When it happens

Trigger: Calling Create(type) during deserialization for a type whose parameterless constructor exists but throws (e.g. constructor reads a missing config file, or throws NotSupportedException); passing a generic type definition like typeof(List<>) to Activator.CreateInstance; passing an abstract/interface type that was misclassified as concrete.

Common situations: YAML files referencing plugin types whose constructors depend on environment state that is absent at load time; deserializing into open generic or abstract types after a schema/type rename; assembly load failures surfacing inside the constructor.

Related errors


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

Appendix: source

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

        /// <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
        {
            public InstanceCreationException(string message) : base(message) { }
            public InstanceCreationException(string message, Exception innerException) : base(message, innerException) { }
        }
    }
}

View on GitHub (pinned to 96fad776d2)