MonoGame/MonoGame · error · ArgumentException

Invalid type has been provided for GenericCollectionHelper:

Error message

Invalid type has been provided for GenericCollectionHelper: {type}

What it means

GenericCollectionHelper expects a type implementing exactly one ICollection<T> so it can resolve the element type, the Count property, and the Add method. GetCollectionElementType returns null when the type implements zero or more than one ICollection<> interface, and the constructor treats that as a programmer error (ArgumentException), not a content error.

Source

Thrown at MonoGame.Framework.Content.Pipeline/Serialization/Intermediate/GenericCollectionHelper.cs:41

        {
            var interfaces = type.FindInterfaces((t, _) =>
            {
                if (t.IsGenericType)
                    return t.GetGenericTypeDefinition() == typeof(ICollection<>);
                return false;
            }, null);

            return (interfaces.Length == 1) ? interfaces[0] : null;
        }

        private readonly ContentTypeSerializer _contentSerializer;
        private readonly PropertyInfo _countProperty;
        private readonly MethodInfo _addMethod;

        public GenericCollectionHelper(IntermediateSerializer serializer, Type type)
        {
            var collectionElementType = GetCollectionElementType(type, false) ??
                throw new ArgumentException($"Invalid type has been provided for GenericCollectionHelper: {type}", nameof(type));
            _contentSerializer = serializer.GetTypeSerializer(collectionElementType);

            var collectionType = typeof(ICollection<>).MakeGenericType(collectionElementType);
            _countProperty = collectionType.GetProperty("Count")!;
            _addMethod = collectionType.GetMethod("Add", [collectionElementType])!;
        }

        public bool ObjectIsEmpty(object? list)
        {
            if (list == null)
                return true;

            if (_countProperty.GetValue(list, null) is not int listCount)
                return true;

            return listCount == 0;
        }

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Make the collection type implement exactly one ICollection<T> for the element type you want serialized.
  2. If multiple element types are involved, split into separate collection classes or wrap the desired element type.
  3. If you do not want generic-collection serialization semantics, decorate members with [ContentSerializerIgnore] or provide a custom ContentTypeSerializer.

Example fix

// before
public class MixedBag : ICollection<int>, ICollection<string> { ... }

// after
public class IntBag : ICollection<int> { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

// Verify a type is a valid single-element generic collection before exposing it to the serializer
static bool IsValidSingleElementCollection(Type t)
{
    var ifaces = t.FindInterfaces((tp, _) =>
        tp.IsGenericType && tp.GetGenericTypeDefinition() == typeof(ICollection<>), null);
    return ifaces.Length == 1;
}

Type guard

static bool IsSerializableCollection(Type t) =>
    t.FindInterfaces((tp, _) =>
        tp.IsGenericType && tp.GetGenericTypeDefinition() == typeof(ICollection<>), null).Length == 1;

Try / catch

try { serializer.GetCollectionHelper(type); }
catch (ArgumentException ex) when (ex.Message.Contains("GenericCollectionHelper"))
{
    // redesign the type to implement exactly one ICollection<T>
}

Prevention

When it happens

Trigger: The reflective serializer or GetCollectionHelper is handed a type that either (a) implements no generic ICollection<T>, or (b) implements two or more distinct ICollection<T> (e.g. `class Bag : ICollection<int>, ICollection<string>`). FindCollectionInterface returns null when interfaces.Length != 1.

Common situations: Authoring a custom collection type meant to be serialized that doesn't implement ICollection<T>, or that ambiguously implements several. Usually a design-time bug in a pipeline extension type, surfaced the first time that type is serialized.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/dcd2bea46ec7eb49. Report an issue: GitHub.