FxCop警告CA1006是否有任何好的解决方法? [英] Are there any good workarounds for FxCop warning CA1006?
问题描述
I am having trouble with FxCop warning CA1006, Microsoft.Design "DoNotNestGenericTypesInMemberSignatures". Specifically, I am designing a ReportCollection<T>
class that inherits from ReadOnlyCollection<Report<T>>
, and its public
constructor takes an IList<Report<T>>
as a parameter.
解决此警告的建议不是很有用:
The suggestion for fixing this warning is not very useful:
要解决违反此规则的问题,更改设计以删除嵌套的类型参数。到目前为止,我可以通过两种方式更改建议的设计:
"To fix a violation of this rule, change the design to remove the nested type argument." There are two ways I can see so far to change the design as suggested:
- 使构造函数
内部
。就我而言,这不起作用。构造函数必须为public
,因为此集合类需要由程序集外部的代码实例化。 - 使构造函数采用
Report< T> []
,而不是IList< Report< T>>
。这不是最佳选择,因为外部代码应该具有使用动态大小的数据结构(例如List< T>
)而不是固定大小的数组的灵活性。
- Make the constructor
internal
. This doesn't work in my case. The constructor must bepublic
because this collection class needs to be instantiable by code outside the assembly. - Make the constructor take a
Report<T>[]
instead of anIList<Report<T>>
. This is sub-optimal because external code should have the flexibility of using dynamically-sized data structures likeList<T>
instead of fixed-size arrays.
这时,我已经放弃并取消了此警告。有更好的解决方案吗?
At this point, I have given up and suppressed this warning. Is there a better solution?
推荐答案
我同意,另一个忽略此规则的好时机是您需要说: p>
I agree, another good time to ignore this rule is when you need to say:
Func<IEnumerable<T>>
当然,您可以使用非泛型IEnumerable,但是只要可以使用任何类型它实现IEnumerable(非泛型)。泛型(部分)的目的是将允许的类型限制为给定的一组类型。
of course you could use the non-generic IEnumerable, but then any type can be used as long as it implements IEnumerable (non-generic). The purpose of generics (in part) is to restrict the types that are permisable to a given set of types.
我认为这条规则非常愚蠢。仅当您嵌套了多个泛型类型时才需要它。
I think this rule is very stupid. only need it if you have multiple generic types nested. one layer of nesting is more than safe.
BTW,我认为很多LINQ函数也嵌套通用类型,所以如果MS做到这一点,我们也可以: )
BTW, I think a lot of the LINQ Functions nest generic types as well, so if MS does it, we can too :)
这篇关于FxCop警告CA1006是否有任何好的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!