SA1125:为什么强制执行"int?" 而不是"Nullable <int>"而不是"typeof()"?

Kna*_*ģis 6 c# stylecop

SA1125:UseShorthandForNullableTypes具有此描述(取自StyleCop 4.7设置编辑器应用程序):

强制使用可空类型的简写而不是a中的Nullable<T>except typeof().

有没有理由说它有typeof()声明的例外?typeof(int?)编译同样好 - 这只是StyleCop作者的偏好还是有更深层次的推理?

编辑:由于官方文档没有提到这个例外,我测试了以下代码:

var x = new Nullable<int>();
var y = new int?();
var z = typeof(Nullable<int>);
var v = typeof(int?);
Run Code Online (Sandbox Code Playgroud)

结果:只有第一行引发SA1125警告.

编辑2:StyleCop的工作项要求修复此行为

Ada*_*rth 0

我的观点是,这最多是一个错误,至少是一个错过的行为。代码指出:

// Check the declaration of the generic type for longhand, but allow Nullable<> which has no shorthand
if (genericType.ChildTokens.Count > 0 && Utils.TokenContainNullable(genericType.ChildTokens.First))
{
    if (genericType.Parent == null || !(genericType.Parent is TypeofExpression))
    {
Run Code Online (Sandbox Code Playgroud)

看起来它正在尝试Nullable<>支持typeof(Nullable<>). 然而,检查TypeofExpression无意中过滤掉了封闭的泛型,没有明显的原因。

寻找CheckShorthandForNullableTypes

http://stylecop.codeplex.com/SourceControl/changeset/view/249eed5b15ed#Project/Src/AddIns/CSharp/Analyzers/ReadabilityRules.cs