FxCop,从依赖程序集中组成调用者列表

发布于 2024-12-06 00:58:43 字数 192 浏览 0 评论 0原文

我正在构建几个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2024-12-13 00:58:43

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.

-小熊_ 2024-12-13 00:58:43

我想我没有很好地解释这个问题,但我明白你的意思。
我有 3 个不同的程序集,对于从一个程序集到另一个程序集的某些方法调用,我需要确保调用基准构造函数。基准类位于第四个程序集中。现在我的问题是,只有 VS2010 只加载一个目标程序集进行分析,当我使用 CallGraph 构建调用构造函数的方法列表时,它找不到任何目标程序集。手动调用 FxCopCmd.exe 时,我可以使用 /file: 参数手动添加依赖程序集。

我的解决方案是手动加载不同的程序集(不依赖于 RuleUtilities.AnalysisAssemblies 中加载

            RuleUtilities.GetAssembly(
            RuleUtilities.AnalysisAssemblies
            .First().Directory + "\\" + additionalAssemblyFilename)
            .Types.SelectMany(type => type.Members)
            .Where(member => member.IsPublic)
            .Where(CanBeCastedToMethod)
            .Cast<Method>()
            .SelectMany(CallGraph.CallersFor);

的程序集,并在 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.

            RuleUtilities.GetAssembly(
            RuleUtilities.AnalysisAssemblies
            .First().Directory + "\\" + additionalAssemblyFilename)
            .Types.SelectMany(type => type.Members)
            .Where(member => member.IsPublic)
            .Where(CanBeCastedToMethod)
            .Cast<Method>()
            .SelectMany(CallGraph.CallersFor);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文