更好的方式来声明类的泛型参数

huangapple go评论59阅读模式
英文:

Better way to declare generic parameters on class

问题

I'm writing a FluentValidation custom validator to validate that two read-only collections have the same number of elements.

I'm using VS2022 and FluentValidation v11.5.2.

I currently have the following:

public class CollectionSizesMatchValidator<T, TProperty, TElement> : PropertyValidator<T, TProperty>
    where TProperty : IReadOnlyCollection<TElement>

But I'm getting a CA1005 message: Avoid excessive parameters on generic types.

I thought this might be a better solution, but it doesn't compile:

public class CollectionSizesMatchValidator<T, IReadOnlyCollection<TElement>> : PropertyValidator<T, IReadOnlyCollection<TElement>>

I don't like the warning generated by the compiler, so I'd like to reduce the number of generic types. I thought I could use ICollection, but IReadOnlyCollection doesn't "inherit" from ICollection.

When I use the validator, I have to supply all the types.

Is there a better solution than the one I currently have?

英文:

I'm writing a FluentValidation custom validator to validate that two read only collections have the same number of elements.

I'm using VS2022 and FluentValidation v11.5.2.

I current have the following:

public class CollectionSizesMatchValidator&lt;T, TProperty, TElement&gt; : PropertyValidator&lt;T, TProperty&gt;
	where TProperty : IReadOnlyCollection&lt;TElement&gt;

but I'm getting a CA1005 message: Avoid excessive parameters on generic types.

I thought this might be a better solution but it doesn't compile

public class CollectionSizesMatchValidator&lt;T, IReadOnlyCollection&lt;TElement&gt;&gt; : PropertyValidator&lt;T, IReadOnlyCollection&lt;TElement&gt;&gt;

I don't like the warning generated by the compiler, so I'd like to reduce the number of generic types. I thought I could use ICollection, but IReadOnlyCollection<T> doesn't "inherit" from ICollection

When I use the validator I have to supply all the types.

Is there a better solution than the one I currently have?

答案1

得分: 2

在你的第二次尝试中,你需要将TElement指定为泛型参数,而不是IReadOnlyCollection&lt;...&gt;,然后在PropertyValidator&lt;T, IReadOnlyCollection&lt;TElement&gt;&gt;中使用它:

public class CollectionSizesMatchValidator&lt;T, TElement&gt; : PropertyValidator&lt;T, IReadOnlyCollection&lt;TElement&gt;&gt;
{
    public override bool IsValid(ValidationContext&lt;T&gt; context, IReadOnlyCollection&lt;TElement&gt; value)
    {
        throw new NotImplementedException();
    }

    public override string Name { get; }
}

或者,只需按照文档中的说明禁用警告。

英文:

In your second attempt you need to specify TElement as generic parameter, not IReadOnlyCollection&lt;...&gt; and then use it for PropertyValidator&lt;T, IReadOnlyCollection&lt;TElement&gt;&gt;:

public class CollectionSizesMatchValidator&lt;T, TElement&gt; : PropertyValidator&lt;T, IReadOnlyCollection&lt;TElement&gt;&gt;
{
    public override bool IsValid(ValidationContext&lt;T&gt; context, IReadOnlyCollection&lt;TElement&gt; value)
    {
        throw new NotImplementedException();
    }

    public override string Name { get; }
}

Or just disable the warning as explained in the docs.

huangapple
  • 本文由 发表于 2023年5月18日 02:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定