{"record":{"id":"6be5ed2e5932fb1a","repo":"stride3d/stride","slug":"the-given-type-does-not-implement-iobjectfactory","errorCode":null,"errorMessage":"The given type does not implement IObjectFactory/","messagePattern":"The given type does not implement IObjectFactory/","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Annotations/ObjectFactoryAttribute.cs","lineNumber":34,"sourceCode":"{\n    /// <summary>\n    /// The type of the factory to use to create instance of the related type.\n    /// </summary>\n    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]\n    public Type FactoryType { get; }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"ObjectFactoryAttribute\"/> class.\n    /// </summary>\n    /// <param name=\"factoryType\">The factory type that implements <see cref=\"IObjectFactory\"/>.</param>\n    public ObjectFactoryAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type factoryType)\n    {\n#if NET7_0_OR_GREATER\n        ArgumentNullException.ThrowIfNull(factoryType);\n#else\n        if (factoryType is null) throw new ArgumentNullException(nameof(factoryType));\n#endif // NET7_0_OR_GREATER\n        if (!typeof(IObjectFactory).GetTypeInfo().IsAssignableFrom(factoryType.GetTypeInfo())) throw new ArgumentException($\"The given type does not implement {nameof(IObjectFactory)}/\", nameof(factoryType));\n        if (factoryType.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null) throw new ArgumentException(\"The given type does have a public parameterless constructor.\", nameof(factoryType));\n        FactoryType = factoryType;\n    }\n}\n","sourceCodeStart":16,"sourceCodeEnd":39,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Annotations/ObjectFactoryAttribute.cs#L16-L39","documentation":"ObjectFactoryAttribute declares which factory type creates instances of a class for Stride's serialization system; the supplied type must implement Stride.Core.IObjectFactory. The constructor verifies IObjectFactory.IsAssignableFrom(factoryType) and throws ArgumentException when it does not, because the factory could never be invoked later.","triggerScenarios":"[ObjectFactory(typeof(X))] where X does not implement IObjectFactory; passing the product/entity type instead of the factory type; a refactor where the class stopped implementing IObjectFactory (interface moved/renamed across Stride versions).","commonSituations":"Writing custom serialization factories in Stride; upgrading Stride where IObjectFactory's namespace or shape changed; typo or autocomplete selecting the wrong type in the attribute.","solutions":["Make the factory class implement Stride.Core.IObjectFactory (and its members).","Pass the factory type, not the type being constructed, in the attribute.","Verify with typeof(IObjectFactory).IsAssignableFrom(candidateType) before applying the attribute."],"exampleFix":"// before\n[ObjectFactory(typeof(SpriteGroup))] // wrong: the entity, not the factory\npublic class SpriteGroup { ... }\n\n// after\n[ObjectFactory(typeof(SpriteGroupFactory))] // SpriteGroupFactory : IObjectFactory\npublic class SpriteGroup { ... }","handlingStrategy":"validation","validationCode":"if (!typeof(IObjectFactory).IsAssignableFrom(factoryType))\n    throw new ArgumentException($\"{factoryType} must implement IObjectFactory\", nameof(factoryType));\nvar attr = new ObjectFactoryAttribute(factoryType);","typeGuard":"static bool IsObjectFactory(Type t) => typeof(IObjectFactory).IsAssignableFrom(t);","tryCatchPattern":null,"preventionTips":["Have factory classes explicitly implement Stride.Core.IObjectFactory.","Double-check that the attribute receives the factory type, not the product type.","Re-verify factory implementations after Stride version upgrades (interface may have moved)."],"tags":["csharp","stride-core","serialization","argument-validation"],"backgroundTag":"type-mismatch","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"}