确定方法是否调用包含新语句的另一个程序集中的方法,反之亦然
我想编写一个规则,如果在标记有特定属性的方法调用的任何方法中进行对象分配,该规则将会失败。
到目前为止,我已经完成了这项工作,通过迭代调用我的方法的所有方法来使用 CallGraph.CallersFor() 进行检查,以查看这些父方法中是否有任何一个具有该属性。
这适用于检查与要检查的方法相同的程序集中的父方法,但是在线阅读时,似乎有一次 CallGraph.CallersFor()
确实查看了所有程序集,但现在它没有。
问题:有没有办法获取调用给定方法的方法列表,包括不同程序集中的方法?
替代答案:如果上述不可能,我如何循环遍历给定方法调用的每个方法,包括不同程序集中的方法。
示例:
-----In Assembly A
public class ClassA
{
public MethodA()
{
MethodB();
}
public MethodB()
{
object o = new object(); // Allocation i want to break the rule
// Currently my rule walks up the call tree,
// checking for a calling method with the NoAllocationsAllowed attribute.
// Problem is, because of the different assemblies,
// it can't go from ClassA.MethodA to ClassB.MethodB.
}
}
----In Assembly B
public var ClassAInstance = new ClassA();
public class ClassB
{
[NoAllocationsAllowed] // Attribute that kicks off the rule-checking.
public MethodA()
{
MethodB();
}
public MethodB()
{
ClassAInstance.MethodA();
}
}
我并不介意规则在哪里报告错误,在这个阶段获取错误就足够了。
I want to write a rule that will fail if an object allocation is made within any method called by a method marked with a particular attribute.
I've got this working so far, by iterating up all methods calling my method to check using CallGraph.CallersFor()
, to see if any of those parent methods have the attribute.
This works for checking parent methods within the same assembly as the method to be checked, however reading online, it appears that at one time CallGraph.CallersFor()
did look at all assemblies, however now it does not.
Question: Is there a way of getting a list of methods that call a given method, including those in a different assembly?
Alternative Answer: If the above is not possible, how do i loop through every method that is called by a given method, including those in a different assembly.
Example:
-----In Assembly A
public class ClassA
{
public MethodA()
{
MethodB();
}
public MethodB()
{
object o = new object(); // Allocation i want to break the rule
// Currently my rule walks up the call tree,
// checking for a calling method with the NoAllocationsAllowed attribute.
// Problem is, because of the different assemblies,
// it can't go from ClassA.MethodA to ClassB.MethodB.
}
}
----In Assembly B
public var ClassAInstance = new ClassA();
public class ClassB
{
[NoAllocationsAllowed] // Attribute that kicks off the rule-checking.
public MethodA()
{
MethodB();
}
public MethodB()
{
ClassAInstance.MethodA();
}
}
I don't really mind where the rule reports the error, at this stage getting the error is enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过在 FxCop 项目中添加所有引用的 dll 并使用下面的代码来解决这个问题,该代码手动构建调用树(它还添加对派生类的调用来解决我遇到的另一个问题,此处。
I got round this issue by adding all referenced dlls in my FxCop project, and using the code below, which builds a call tree manually (it also adds calls for derived classes to work round another problem i encountered, here.
你有没有尝试过这样的方式
Did you give it a try in this way,