是否有一个合理的简单的方法来让FxCop检查是否有任何使用ViewBag, ViewData在MVC应用程序
最简单的方法是筛选名称为"ViewBag"或"ViewData"(带有get_
和set_
前缀)的MemberBinding实例。这可能会导致相当多的误报,因此下一个复杂性/完整性级别将是检查MemberBinding目标的类型,看看它是视图还是控制器。例如:
private readonly string[] _forbiddenNames = new string[] { "get_ViewBag", "set_ViewBag", "get_ViewData", "set_ViewData" };
public override ProblemCollection Check(Member member)
{
var method = member as Method;
if (method != null)
{
this.Visit(method.Body);
}
return this.Problems;
}
public override void VisitMemberBinding(MemberBinding memberBinding)
{
base.VisitMemberBinding(memberBinding);
var memberName = memberBinding.BoundMember.Name.Name;
if (this._forbiddenNames.Contains(memberName))
{
this.Problems.Add(new Problem(this.GetResolution(memberBinding.BoundMember), memberBinding, memberName));
}
}