c# - 根据 FxCop,为什么 ReadOnlyCollection<ReadOnlyCollection<T>> 不好?在生成不可变二维对象时有什么替代方案?

标签 c# .net immutability fxcop readonly-collection

<分区>

我正在修改我的所有代码以符合 FxCop,这意味着放弃大量数组和列表以支持 ReadOnlyCollection,我同意这个建议。然而,当生产一个

ReadOnlyCollection<ReadOnlyCollection<T>> 

替换二维数组或

List<List<T>> I now get the 

CA1006: Do not nest generic types in member signatures

投诉。首先,虽然它看起来很笨重,但它看起来并不复杂或难以理解,因为它本质上是一个不可变的 List<List<T>>。 ,考虑到数组的缺点,我认为这是非常普遍的。 其次,我想不出存储二维数据且不可变的替代方案,除非我专门为此创建一个新类型。

请问这里的最佳做法是什么。难道这个 FxCop 规则在这里并不适用,应该被禁止吗?

非常感谢。

最佳答案

这篇文章的评论似乎也适用于此:

The warning is a general warning supposed to help you design a better and simpler public interface. In this case you get a warning about having a Expression> parameter in your method. However, for this method there is no point in simplifying the type and instead you should suppress the warning using an attribute or completely remove the rule from your ruleset.

A silly example where you probably should consider following the advice of the rule is a method like this:

public void F(Dictionary<String, List<Tuple<<String, Int32>>> dictionary);

Martin Liversage 的回答@ https://stackoverflow.com/a/14945331/1775528

关于c# - 根据 FxCop,为什么 ReadOnlyCollection<ReadOnlyCollection<T>> 不好?在生成不可变二维对象时有什么替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618186/

相关文章:

c# - 在 C# 中检查代码重复的工具

c# - 如何将空值从 TextBox 插入到整数字段

c# - 获取唯一字符串 C#

kotlin - 为什么要始终使用不可变集合接口(interface)来声明在 Kotlin 中未发生变异的集合?

c# - XDocument 保存删除节点前缀

c# - 有使用SandcaSTLe生成API文档的快速入门指南吗?

c# second form 初始化后关闭

c# - 从现有数据库向 DbContext 添加表?

javascript - 如何在没有突变的情况下使用 javascript Map

java - 将 java 对象标记为final(实际上是final的)有什么好处吗?