当我在签入时运行代码分析时,我收到 SA0102(“文件中发现语法错误”)错误(另一个奇怪的问题,因为我认为 SA 是 StyleCop 问题?!)。通过添加/删除代码的过程,我推断出以下方法导致抛出此错误。据我所知,此方法是有效的,并且基于其他人的评论 - StyleCop 中似乎存在一些围绕此规则的错误。
无论如何,我的问题是如何抑制这个警告?我尝试将文件名更改为 .designer.cs,但这似乎没有效果(我已确保选中忽略设计器文件)。我对如何解决这个问题有点迷茫,因为如果不通过 StyleCop 签入政策,我就无法签入代码!另外,
作为参考,这是导致问题的方法 - 我怀疑在声明中使用可空值可能是问题所在。
编辑:我正在使用 StyleCop v4.4.1.2
编辑:如果我删除 IEnumerable>已选择,整数?根据参数 page = null,规则通过。
[HttpPost]
public ActionResult Search(string searchCriteria, IEnumerable`<int?`> selected, int? page = null)
{
if (page.HasValue)
{
const int PageSize = 6;
IEnumerable<MyClass> src = this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria));
string rows = this.RenderView(@"Awesome\LookupList", src.Skip((page.Value - 1) * PageSize).Take(PageSize));
return this.Json(new { rows, more = src.Count() > page * PageSize });
}
return this.View(@"Awesome\LookupList", this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria)));
}
I am getting an in SA0102 ('A syntax error has been discovered in the file') error being thrown when I run Code Analysis upon check-in (another strange issue as I thought SA was a StyleCop issue?!). Through a process of adding/removing code, I have deduced that the following method is causing this error to be thrown. As far as I am aware, this method is valid and based on comments from other - there appear to be some bugs in StyleCop surrounding this rule.
Anyway, my question is how can I suppress this warning? I have tried changing the filename to .designer.cs, however this appears to have no effect (I have made sure ignore designer files is checked). I am a little lost as to how I can resolve this issue as I can't check-in the code without passing the StyleCop check-in policy! Also, this
For reference, this is the method causing the issue - I suspect the use of nullables in the declaration may be the problem.
Edit: I am using StyleCop v4.4.1.2
Edit: If I remove the IEnumerable<int?
> selected, int? page = null from the parameters, the rule passes.
[HttpPost]
public ActionResult Search(string searchCriteria, IEnumerable`<int?`> selected, int? page = null)
{
if (page.HasValue)
{
const int PageSize = 6;
IEnumerable<MyClass> src = this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria));
string rows = this.RenderView(@"Awesome\LookupList", src.Skip((page.Value - 1) * PageSize).Take(PageSize));
return this.Json(new { rows, more = src.Count() > page * PageSize });
}
return this.View(@"Awesome\LookupList", this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria)));
}
发布评论
评论(2)
好的,如果参数中使用了可空类型,就会导致问题。
OK, if would appear to be the use of nullable types in the parameter that is causing the issue.
刚刚用 4.4.1.2 检查了您的示例,它工作正常。
您确定您的签到政策使用 4.4.1.2 吗?它在服务器上运行,而不是在您的本地计算机上 - 因此您可能安装了 StyleCop 4.4.1.2,但服务器使用其他版本。
以前的 StyleCop 版本(例如 4.4.0.14)确实存在一些与可选参数相关的错误。
Just checked your example with 4.4.1.2 and it works fine.
Are you sure that your check-in policy uses 4.4.1.2? It runs on server, not on your local machine - so you may have StyleCop 4.4.1.2 installed, but server uses some other version.
Previous StyleCop versions (for instance, 4.4.0.14) were having a couple of bugs connected with optional parameters indeed.