是否可以以编程方式将堆栈跟踪递归到特定的调用程序集?

发布于 2024-08-23 10:59:35 字数 732 浏览 4 评论 0原文

我想知道是否可以以编程方式将堆栈跟踪递归到特定程序集。

我正在使用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 技术交流群。

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

发布评论

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

评论(3

时光清浅 2024-08-30 10:59:36

Assembly.GetCallingAssembly 对您有帮助吗?这仅获取调用您从中调用 GetCallingAssembly 的方法的程序集。

Does Assembly.GetCallingAssembly help you? This gets only the assembly which called the method you are calling GetCallingAssembly from.

歌入人心 2024-08-30 10:59:36

我为单元测试做了类似的事情。我有一个实用程序,在失败时会将失败信息写入以调用此函数的单元测试命名的文件中。我使用了这样的代码:

    private static string ExtractTestMethodNameFromStackFrame(StackFrame frame)
    {
        string outputName = null;
        MethodBase method = frame.GetMethod();
        Object[] myAttributes = method.GetCustomAttributes(false);
        foreach (Object attrib in myAttributes)
        {
            //NUnit Specific
            if (attrib is NUnit.Framework.TestAttribute)
            {
                outputName = method.Name;
                break;
            }
        }
        return outputName;
    }

    private static string GetCallingTestInformation(out string moduleName)
    {
        moduleName = null;
        string outputName = null;
        StackTrace st = new StackTrace(false);
        Exception internalException = null;
        try
        {
            StackFrame[] frames = st.GetFrames();

            for (int i = 0; i < frames.Length; i++)
            {
                if (moduleName != null && outputName != null)
                    break;

                StackFrame frame = frames[i];


                if (moduleName == null)
                {
                    moduleName = ExtractTestModuleNameFromStackFrame(frame);
                }

                if (outputName == null)
                {
                    outputName = ExtractTestMethodNameFromStackFrame(frame);
                }
            }
        }
        catch (Exception ex)
        {
            internalException = ex;
        }

        if (outputName == null && moduleName == null)
            throw new TestUtilityException("Failed to find Test method or module name: " + st.ToString(), internalException);
        return outputName;
    }

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:

    private static string ExtractTestMethodNameFromStackFrame(StackFrame frame)
    {
        string outputName = null;
        MethodBase method = frame.GetMethod();
        Object[] myAttributes = method.GetCustomAttributes(false);
        foreach (Object attrib in myAttributes)
        {
            //NUnit Specific
            if (attrib is NUnit.Framework.TestAttribute)
            {
                outputName = method.Name;
                break;
            }
        }
        return outputName;
    }

    private static string GetCallingTestInformation(out string moduleName)
    {
        moduleName = null;
        string outputName = null;
        StackTrace st = new StackTrace(false);
        Exception internalException = null;
        try
        {
            StackFrame[] frames = st.GetFrames();

            for (int i = 0; i < frames.Length; i++)
            {
                if (moduleName != null && outputName != null)
                    break;

                StackFrame frame = frames[i];


                if (moduleName == null)
                {
                    moduleName = ExtractTestModuleNameFromStackFrame(frame);
                }

                if (outputName == null)
                {
                    outputName = ExtractTestMethodNameFromStackFrame(frame);
                }
            }
        }
        catch (Exception ex)
        {
            internalException = ex;
        }

        if (outputName == null && moduleName == null)
            throw new TestUtilityException("Failed to find Test method or module name: " + st.ToString(), internalException);
        return outputName;
    }
野却迷人 2024-08-30 10:59:35

您需要使用 StackTrace 类。

例如:

var structureMapFrame = new StackTrace()
    .GetFrames()
    .FirstOrDefault(f => f.GetMethod().ToString()
              .IndexOf("structuremap", StringComparison.OrdinalIgnoreCase) >= 0)

You need to use the StackTrace class.

For example:

var structureMapFrame = new StackTrace()
    .GetFrames()
    .FirstOrDefault(f => f.GetMethod().ToString()
              .IndexOf("structuremap", StringComparison.OrdinalIgnoreCase) >= 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文