stride3d/stride · error · ArgumentException

The given type does not implement IObjectFactory/

Error message

The given type does not implement IObjectFactory/

What it means

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.

Solutions

  1. Make the factory class implement Stride.Core.IObjectFactory (and its members).
  2. Pass the factory type, not the type being constructed, in the attribute.
  3. Verify with typeof(IObjectFactory).IsAssignableFrom(candidateType) before applying the attribute.

Example fix

// before
[ObjectFactory(typeof(SpriteGroup))] // wrong: the entity, not the factory
public class SpriteGroup { ... }

// after
[ObjectFactory(typeof(SpriteGroupFactory))] // SpriteGroupFactory : IObjectFactory
public class SpriteGroup { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (!typeof(IObjectFactory).IsAssignableFrom(factoryType))
    throw new ArgumentException($"{factoryType} must implement IObjectFactory", nameof(factoryType));
var attr = new ObjectFactoryAttribute(factoryType);

Type guard

static bool IsObjectFactory(Type t) => typeof(IObjectFactory).IsAssignableFrom(t);

Prevention

When it happens

Trigger: [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).

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core/Annotations/ObjectFactoryAttribute.cs:34

{
    /// <summary>
    /// The type of the factory to use to create instance of the related type.
    /// </summary>
    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
    public Type FactoryType { get; }

    /// <summary>
    /// Initializes a new instance of the <see cref="ObjectFactoryAttribute"/> class.
    /// </summary>
    /// <param name="factoryType">The factory type that implements <see cref="IObjectFactory"/>.</param>
    public ObjectFactoryAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type factoryType)
    {
#if NET7_0_OR_GREATER
        ArgumentNullException.ThrowIfNull(factoryType);
#else
        if (factoryType is null) throw new ArgumentNullException(nameof(factoryType));
#endif // NET7_0_OR_GREATER
        if (!typeof(IObjectFactory).GetTypeInfo().IsAssignableFrom(factoryType.GetTypeInfo())) throw new ArgumentException($"The given type does not implement {nameof(IObjectFactory)}/", nameof(factoryType));
        if (factoryType.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null) throw new ArgumentException("The given type does have a public parameterless constructor.", nameof(factoryType));
        FactoryType = factoryType;
    }
}

View on GitHub (pinned to 96fad776d2)