Pur*_*ome 0 .net reflection recursion
我想知道是否可以以编程方式将堆栈跟踪递归到特定程序集。
我正在使用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??).
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
这个问题与这个SO 问题密切相关......我最终根据这里的答案添加了自己的答案:)
您需要使用该类StackTrace。
例如:
var structureMapFrame = new StackTrace()
.GetFrames()
.FirstOrDefault(f => f.GetMethod().ToString()
.IndexOf("structuremap", StringComparison.OrdinalIgnoreCase) >= 0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
205 次 |
| 最近记录: |