{"record":{"id":"ac46087b23d62216","repo":"stride3d/stride","slug":"typeof-activator-failed-to-create-instance-of-type-type-see","errorCode":null,"errorMessage":"'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.","messagePattern":"'(.+?)' failed to create instance of type '(.+?)', see inner exception\\.","errorType":"exception","errorClass":"InstanceCreationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs","lineNumber":119,"sourceCode":"\n        /// <inheritdoc/>\n        public object Create(Type type)\n        {\n            type = GetDefaultImplementation(type);\n\n            // We can't instantiate primitives or arrays\n            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)\n                throw new InstanceCreationException($\"Failed to create instance of type '{type}', wrong factory.\");\n\n            if (type.GetConstructor(EmptyTypes) != null || type.IsValueType)\n            {\n                try\n                {\n                    return Activator.CreateInstance(type);\n                }\n                catch (Exception e)\n                {\n                    throw new InstanceCreationException($\"'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.\", e);\n                }\n            }\n\n            throw new InstanceCreationException($\"Failed to create instance of type '{type}', type does not have a parameterless constructor.\");\n        }\n\n        public class InstanceCreationException : Exception\n        {\n            public InstanceCreationException(string message) : base(message) { }\n            public InstanceCreationException(string message, Exception innerException) : base(message, innerException) { }\n        }\n    }\n}\n","sourceCodeStart":101,"sourceCodeEnd":133,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs#L101-L133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the InnerException to find the actual failure inside the type's constructor and fix that cause","Ensure the deserialized type is a closed, concrete, instantiable type (not abstract, interface, or open generic)","Register a custom IObjectFactory that knows how to construct the problematic type instead of relying on the parameterless constructor","Give the type a public parameterless constructor that does not perform environment-dependent side effects"],"exampleFix":"// before\nvar entity = serializer.Deserialize(stream, typeof(PluginBase)); // abstract -> throws\n// after\nvar entity = serializer.Deserialize(stream, typeof(AudioPlugin)); // concrete closed type","handlingStrategy":"try-catch","validationCode":"if (type == null || type.IsAbstract || type.IsInterface || type.IsGenericTypeDefinition || type.GetConstructor(Type.EmptyTypes) == null)\n    throw new InvalidOperationException($\"Type {type} cannot be instantiated via parameterless ctor\");","typeGuard":"static bool IsInstantiable(Type t) => t != null && !t.IsAbstract && !t.IsInterface && !t.IsGenericTypeDefinition && t.GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { var obj = serializer.Deserialize(stream, type); }\ncatch (DefaultObjectFactory.InstanceCreationException ex) { log(ex.InnerException); throw new LoadException($\"Cannot create {type}\", ex); }","preventionTips":["Keep deserialized POCO constructors side-effect free","Never pass abstract/interface/open-generic types as the target type","Register custom factories for types needing constructor arguments","Always inspect InnerException, not just the wrapper message"],"tags":["yaml","serialization","reflection","instantiation"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}