FxCop,从依赖程序集中组成调用者列表
我正在构建几个自定义 FxCop 规则,其中一个规则需要强制在特定方法中调用构造函数。为此,我需要在执行实际测试之前创建特定构造函数的调用者列表。这怎么可能?是否有某种句柄来获取 ApplicationDomain 中所有已加载程序集的列表,我可以在其中迭代这些类并找到构造函数 Method 对象?理想情况下,调用者列表应在 BeforeAnalysis 方法中组成。
I'm building a couple of customs FxCop rules and one of the rules needs to enforce that a constructor is called in specific methods. For that, I need to create a list of callers, to that specific constructor, prior to performing the actual test. How is this possible? Is there some kind of handle to acquire a list of all loaded assemblies in the ApplicationDomain, where I can iterate through the classes and find the constructor Method object? Ideally the list of callers should be composed in the BeforeAnalysis method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Microsoft.FxCop.Sdk.CallGraph.CallersFor(Method) 方法可能会给你你想要的。然而,您似乎描述的一般方法很少是一个好主意,因为它通常会将问题分配给错误的目标。例如,在您描述的场景中,可能需要将问题归因于应该但不包含目标构造函数调用的方法。但是,如果您的分析目标是构造函数,则检测到的问题将归因于构造函数而不是应该调用它的方法。
The Microsoft.FxCop.Sdk.CallGraph.CallersFor(Method) method may give you what you want. However, the general approach you seem to be describing is rarely a good idea because it would typically assign the problems to the wrong target. For example, in the scenario you describe, it would presumably be desirable to attribute the problems to the methods that should but do not contain the target contructor call. However, if your analysis target is the constructor, the detected problems will be attributed to the constructor rather than the methods that should have called it.
我想我没有很好地解释这个问题,但我明白你的意思。
我有 3 个不同的程序集,对于从一个程序集到另一个程序集的某些方法调用,我需要确保调用基准构造函数。基准类位于第四个程序集中。现在我的问题是,只有 VS2010 只加载一个目标程序集进行分析,当我使用 CallGraph 构建调用构造函数的方法列表时,它找不到任何目标程序集。手动调用 FxCopCmd.exe 时,我可以使用 /file: 参数手动添加依赖程序集。
我的解决方案是手动加载不同的程序集(不依赖于 RuleUtilities.AnalysisAssemblies 中加载
的程序集,并在 BeforeAnalysis 方法中构造调用者列表。通过这种方法,我可以为每个程序集和基准类构造函数在 VS2010 中完美运行。
I think I haven't explained the question very well, but I see your point.
I have 3 different assemblies and for certain method calls from one assembly to another, I need to ensure that a benchmark constructor invoked. The benchmark class resides in a 4th assembly. Now my problem was that only VS2010 only loads one target assembly for analysis and when I used the CallGraph to construct the a list of methods calling the constructur, it would not find any. When Invoking FxCopCmd.exe manually I could just add the dependent assemblies manually with the /file: parameter.
My solution is to load the different assemblies manually (not relying on the loaded assembly in RuleUtilities.AnalysisAssemblies and contruct the list of callers in the BeforeAnalysis method.
With this approach I can contruct a list of callers, for each of the assemblies and for the benchmark class constructor. Works perfectly i VS2010.