FxCop警告CA1006是否有任何好的解决方法? [英] Are there any good workarounds for FxCop warning CA1006?

查看:112
本文介绍了FxCop警告CA1006是否有任何好的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到FxCop警告CA1006 < /a>,Microsoft.DesignDoNotNestGenericTypesInMemberSignatures。具体来说,我正在设计一个 ReportCollection< T> 类,该类继承自 ReadOnlyCollection< Report< T>> ,并且其 public 构造函数将 IList< Report< T>> 作为参数。

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:




  1. 使构造函数内部。就我而言,这不起作用。构造函数必须为 public ,因为此集合类需要由程序集外部的代码实例化。

  2. 使构造函数采用 Report< T> [] ,而不是 IList< Report< T>> 。这不是最佳选择,因为外部代码应该具有使用动态大小的数据结构(例如 List< T> )而不是固定大小的数组的灵活性。

  1. Make the constructor internal. This doesn't work in my case. The constructor must be public because this collection class needs to be instantiable by code outside the assembly.
  2. Make the constructor take a Report<T>[] instead of an IList<Report<T>>. This is sub-optimal because external code should have the flexibility of using dynamically-sized data structures like List<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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆