问题描述
我创建了 Microsoft 作为示例给出的类:
internal class ParentType
{
public class NestedType
{
public NestedType()
{
}
}
public ParentType()
{
NestedType nt = new();
}
}
但似乎没有生成警告。我使用的是 Visual Studio 2019。此代码正在测试的框架是 .NET 5
解决方法
警告 CA1034 仅在外部可见嵌套类型上报告,这些嵌套类型在另一个外部可见类型中声明。
由于您的封闭类型是 internal
,因此您的嵌套类型也无法从该程序集外部访问,因此不违反规则。
当您也将封闭类型设为 public
时,您将看到正在报告的诊断:
public class ParentType
{
public class NestedType // Warning CA1034
{
}
}
所以我相信你在官方文档的例子中发现了一个错误。
更新:官方文档现在更新了固定的示例代码。