stride3d/stride · error · ArgumentException

The type must be of EntityComponent

Error message

The type must be of EntityComponent

What it means

EntityComponentAttributes.Get(Type) returns cached attribute metadata (e.g. AllowMultipleComponents) but only accepts types deriving from EntityComponent. It throws ArgumentNullException for null and ArgumentException for non-EntityComponent types, since attribute semantics are undefined for other classes.

Solutions

  1. Pass a Type that derives from Stride.Engine.EntityComponent
  2. Add a null check before calling Get
  3. If inspecting arbitrary types, filter with typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) first

Example fix

// before
var attrs = EntityComponentAttributes.Get(someType);
// after
if (someType != null && typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(someType.GetTypeInfo()))
    var attrs = EntityComponentAttributes.Get(someType);
Defensive patterns

Strategy: type-guard

Validate before calling

if (type == null || !typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) return null;

Type guard

static bool IsEntityComponentType(Type t) => t != null && typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo());

Try / catch

try { attrs = EntityComponentAttributes.Get(type); }
catch (ArgumentException ex) when (ex.Message.Contains("EntityComponent")) { /* fall back to default attributes */ }

Prevention

When it happens

Trigger: Calling EntityComponentAttributes.Get(typeof(SomeNonComponentClass)) or passing a null Type; also via EntityComponentCollection.ValidateItem which calls Get on item.GetType().

Common situations: Reflecting over arbitrary scene object types assuming they are components; passing an interface or base type unrelated to EntityComponent; registering custom components that accidentally do not inherit EntityComponent.

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/0d2950e8a3bb5c73. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Engine/Engine/EntityComponentAttributes.cs:47

        /// </summary>
        /// <typeparam name="T">Type of the component</typeparam>
        /// <returns>The attributes for the specified type.</returns>
        public static EntityComponentAttributes Get<T>() where T : EntityComponent
        {
            return GetInternal(typeof(T));
        }

        /// <summary>
        /// Gets the attributes for the specified type.
        /// </summary>
        /// <param name="type">The type of the component.</param>
        /// <returns>The attributes for the specified type.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException">The type must be of EntityComponent;type</exception>
        public static EntityComponentAttributes Get([NotNull] Type type)
        {
            if (type == null) throw new ArgumentNullException(nameof(type));
            if (!typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) throw new ArgumentException(@"The type must be of EntityComponent", nameof(type));
            return GetInternal(type);
        }

        private static EntityComponentAttributes GetInternal([NotNull] Type type)
        {
            EntityComponentAttributes attributes;
            lock (ComponentAttributes)
            {
                if (!ComponentAttributes.TryGetValue(type, out attributes))
                {
                    attributes = new EntityComponentAttributes(type.GetTypeInfo().GetCustomAttribute<AllowMultipleComponentsAttribute>() != null);
                    ComponentAttributes.Add(type, attributes);
                }
            }
            return attributes;
        }
    }
}

View on GitHub (pinned to 96fad776d2)