开发者

Get function name from DLL

I want to get function name of开发者_Go百科 the exception thrown from dll in asp.net.


You can create a StackTrace class from the exception and analyze the frames.

For example:

public void Throw()
{
    throw new MyException();
}

public void CallThrow()
{
    Throw();
}

[Test]
public void GetThrowingMethodName()
{
    try
    {
        CallThrow();
        Assert.Fail("Should have thrown");
    }
    catch (MyException e)
    {
        MethodBase deepestMethod = new StackTrace(e).GetFrame(0).GetMethod();
        string deepestMethodName = deepestMethod.Name;
        Assert.That(deepestMethodName, Is.EqualTo("Throw"));
    }
}


You should be able to make use of the Stack Trace

Exception.StackTrace Property

StackTrace Class

Represents a stack trace, which is an ordered collection of one or more stack frames.

StackFrame Class

A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread. The stack frame always includes MethodBase information, and optionally includes file name, line number, and column number information.

StackFrame information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame objects.


You can try stacktrace object with the help of system.diagnostic namespace here is the linqpad testing source code that you can try out.

void Main()
{
    try {
        test();
    }
    catch(Exception ex) {
        StackTrace st = new StackTrace();
        st.GetFrame(1).GetMethod().Name.Dump();
    }
}

// Define other methods and classes here


public void test()
{
    throw new NotImplementedException();
}


Checkout StackTrace.GetFrame Method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜