泛型类型的 FXCop 违规

Posted

技术标签:

【中文标题】泛型类型的 FXCop 违规【英文标题】:FXCop violation for generic types 【发布时间】:2016-08-15 09:04:22 【问题描述】:

在方法声明中使用 IList<Dictionary<string, string>> 作为参数类型时,发生 FXCop 违规

它不嵌套泛型类型IList<Dictionary<string, string>>

我该如何解决这个问题?

【问题讨论】:

见Are there any good workarounds for FxCop warning CA1006? 还有这个Alternative to nested type of type Expression<Func<T>> 你可以创建一个包含列表的类,但我会忽略这个规则。 【参考方案1】:

原因是:

嵌套类型参数是一种类型参数,也是泛型类型。 要调用签名包含嵌套类型参数的成员, 用户必须实例化一个泛型类型并将该类型传递给 第二种泛型类型的构造函数。所需的程序和 语法很复杂,应该避免。

它可以帮助您设计更简单的界面。您有 3 个案例:

使用SuppressMessage属性 remove the rule from ruleset try to fix violations change the design to remove the nested type argument

你可以试试:

public void Method(Dictionary<string, string> param)

并使用:

var list = new IList<Dictionary<string, string>>();
list.Add(new Dictionary<string, string>"key1", "value1", "key2", "value2");
list.Add(new Dictionary<string, string>"key11", "value11", "key22", "value22");

foreach(var element in list)

    Method(element);

【讨论】:

以上是关于泛型类型的 FXCop 违规的主要内容,如果未能解决你的问题,请参考以下文章

Java泛型

TS泛型类、泛型接口、泛型函数

java泛型——泛型类泛型方法泛型接口

泛型类的基本使用

在 Typescript 的泛型类中初始化泛型类型

JAVA——泛型类和泛型方法(静态方法泛型)