我正在编写一个 FluentValidation 自定义验证器来验证两个只读集合是否具有相同数量的元素。
我正在使用 VS2022 和 FluentValidation v11.5.2.
我目前有以下几点:
public class CollectionSizesMatchValidator<T, TProperty, TElement> : PropertyValidator<T, TProperty>
where TProperty : IReadOnlyCollection<TElement>
但我收到一条 CA1005 消息:避免在泛型类型上使用过多的参数。
我认为这可能是一个更好的解决方案,但它无法编译
public class CollectionSizesMatchValidator<T, IReadOnlyCollection<TElement>> : PropertyValidator<T, IReadOnlyCollection<TElement>>
我不喜欢编译器生成的警告,所以我想减少泛型类型的数量。我以为我可以使用 ICollection,但 IReadOnlyCollection 不会“继承”自 ICollection
当我使用验证器时,我必须提供所有类型。
有没有比我目前拥有的更好的解决方案?