是否可以以编程方式将堆栈跟踪递归到特定的调用程序集?
我想知道是否可以以编程方式将堆栈跟踪递归到特定程序集。
我正在使用StructureMap,它创建特定类的实例,以注入另一个类。 kewl。当我在注入类的构造函数中时,我希望查看父类是什么,并且堆栈跟踪或多或少有一些被调用的结构图方法。
因此,我希望通过递归堆栈跟踪 GetCurrentMethod()
方法来找到调用此结构图注入的方法,直到我们没有结构图类。
类似...
var callingMethod = System.Reflection.MethodBase.GetCurrentMethod();
while (callingMethod.DeclaringType.ToString().Contains("structuremap"))
{
// get parent calling method, from the variable 'callingMethod'.
}
// here means we've recursed high enough or we have no more to go (null??).
有人可以帮忙吗?
更新
这个问题与这个SO问题密切相关...我最终根据这里的答案添加了自己的答案:)
i'm wondering if it is possible to programattically recurse up a stack trace to a particular assembly.
I'm using StructureMap
and it creates an instance of a particular class, to inject into another class. kewl. When i'm in the constructor of the injected class, i wish to see what was the parent class and the stack trace more or less has a score of structuremap methods which are called.
So, i wish to find the method which called this structuremap injection, by recursing up the stack trace GetCurrentMethod()
methods until we don't have a structuremap class.
something like...
var callingMethod = System.Reflection.MethodBase.GetCurrentMethod();
while (callingMethod.DeclaringType.ToString().Contains("structuremap"))
{
// get parent calling method, from the variable 'callingMethod'.
}
// here means we've recursed high enough or we have no more to go (null??).
can someone help?
Update
This question is closely related to this SO question ... which I ended up adding my own answer, based on the answer from here :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Assembly.GetCallingAssembly 对您有帮助吗?这仅获取调用您从中调用 GetCallingAssembly 的方法的程序集。
Does Assembly.GetCallingAssembly help you? This gets only the assembly which called the method you are calling GetCallingAssembly from.
我为单元测试做了类似的事情。我有一个实用程序,在失败时会将失败信息写入以调用此函数的单元测试命名的文件中。我使用了这样的代码:
I did something similar for unit tests. I had a utility that on failure would write out the failure information to a file named after the unit test calling this. I used code like this:
您需要使用
StackTrace
类。例如:
You need to use the
StackTrace
class.For example: