public class NotNullOrEmptyCollectionAttribute : ValidationAttribute { private readonly int _length; public NotNullOrEmptyCollectionAttribute(int length) { _length = length; } public NotNullOrEmptyCollectionAttribute() { _length = -1; } public override bool IsValid(object value) { return value switch { ICollection collection when _length > -1 => collection.Count >= _length, ICollection collection => collection.Count != 0, _ => value is IEnumerable enumerable && enumerable.GetEnumerator().MoveNext() }; } }