如何在源代码中创建具有正确行号的 FxCop Problem() 对象
我创建了一个 FxCop 规则来检查 DateTime.Now 的使用情况。它工作得很好,只是它将有问题的行号报告为方法的开头,而不是实际调用 DateTime.Now 的代码行。我需要做什么才能在 FxCop 报告中获得正确的行号。这是我的代码:
public override void VisitMemberBinding(MemberBinding memberBinding)
{
string name = memberBinding.BoundMember.FullName;
if (name == "System.DateTime.get_Now")
{
Problems.Add(new Problem(GetResolution(), memberBinding.BoundMember.SourceContext));
}
base.VisitMemberBinding(memberBinding);
}
我尝试过memberBinding.SourceContext和memberBinding.BoundMember.SourceContext,两者都返回方法的起始行号。
我可以使用 SourceContext.(Start|End)LineNumber 但使用哪一个呢?看来我只是没有使用正确的 object.SourceContext
I have created an FxCop rule that checks for DateTime.Now use. It works pretty well, except that it reports the offending line number as the start of the method, rather than the line of code that actually calls DateTime.Now. What do I need to do to get the right line number in the FxCop report. Here's my code:
public override void VisitMemberBinding(MemberBinding memberBinding)
{
string name = memberBinding.BoundMember.FullName;
if (name == "System.DateTime.get_Now")
{
Problems.Add(new Problem(GetResolution(), memberBinding.BoundMember.SourceContext));
}
base.VisitMemberBinding(memberBinding);
}
I've tried memberBinding.SourceContext and memberBinding.BoundMember.SourceContext and both return the method's start line number.
I could use SourceContext.(Start|End)LineNumber but which one? Seems that I am just not using the correct object.SourceContext
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
核心问题是FxCop分析引擎没有为成员绑定分配源上下文。但是,它确实为方法调用分配源上下文,因此您可以将 VisitMemberBinding 重写替换为以下 VisitMethodCall 重写:
The core problem is that the FxCop analysis engine does not assign a source context to a member binding. It does, however, assign a source context to a method call, so you could replace your VisitMemberBinding override with the following VisitMethodCall override: