是否有一种相当简单的方法可以让FxCop检查MVC应用程序中是否有ViewBag,ViewData的使用。
答案 0 :(得分:1)
最简单的方法是使用名称“ViewBag”或“ViewData”(带有get_
和set_
前缀)筛选MemberBinding实例。这可能会导致相当多的误报,因此下一级别的复杂性/完整性将是检查MemberBinding目标的类型,以查看它是视图还是控制器。 e.g:
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));
}
}