stride3d/stride · error · InvalidOperationException

The associated asset type does not have a public…

Error message

The associated asset type does not have a public parameterless constructor.

What it means

Stride's DefaultAssetFactory<T>.New() creates a new instance of the associated asset type T. Before instantiating, it verifies that T exposes a public parameterless constructor via reflection; if not, it throws this InvalidOperationException because the factory cannot construct the asset.

Solutions

  1. Add a public parameterless constructor to the asset type used as T
  2. Ensure the type is concrete (not abstract) and the ctor is public
  3. Use a factory/type that supports constructor arguments instead of DefaultAssetFactory

Example fix

// before
public class MyAsset : Asset
{
    public MyAsset(string name) { Name = name; }
}
// after
public class MyAsset : Asset
{
    public MyAsset() { }
    public MyAsset(string name) { Name = name; }
}
Defensive patterns

Strategy: validation

Validate before calling

if (typeof(T).GetConstructor(Type.EmptyTypes) == null) throw new InvalidOperationException($"{typeof(T)} needs a public parameterless ctor");

Type guard

static bool HasDefaultCtor<T>() => typeof(T).GetConstructor(Type.EmptyTypes) != null;

Try / catch

try { asset = factory.New(); } catch (InvalidOperationException ex) { log.Error(ex.Message); }

Prevention

When it happens

Trigger: Calling New() on a factory whose generic type T (an Asset subclass) has no public parameterless constructor — e.g. the type only defines constructors with parameters or its parameterless constructor is private/internal.

Common situations: Registering a custom asset type in the asset factory system where the author added a convenience constructor with required parameters and removed the implicit default constructor; abstract types; compiler-generated types.

Related errors


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

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/DefaultAssetFactory.cs:22

namespace Stride.Core.Assets;

/// <summary>
/// An implementation of the <see cref="AssetFactory{T}"/> class that uses the default public parameterless constructor
/// of the associated asset type.
/// </summary>
/// <typeparam name="T">The type of asset this factory can create.</typeparam>
public class DefaultAssetFactory<T> : AssetFactory<T> where T : Asset
{
    public static T Create()
    {
        return Activator.CreateInstance<T>()!;
    }

    /// <inheritdoc/>
    public override T New()
    {
        if (typeof(T).GetConstructor(Type.EmptyTypes) == null)
            throw new InvalidOperationException("The associated asset type does not have a public parameterless constructor.");

        return Create();
    }
}

View on GitHub (pinned to 96fad776d2)