Get function name from DLL

I want to get the name of an exception function inferred from a dll in asp.net.

+3
source share
4 answers

You can use stack trace

Exception.StackTrace Property

Class stacktrace

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

Class stackframe

A StackFrame call stack is created and pushed for each function call executed during the execution of the thread. The stack frame always includes Information about the database method and, optionally, includes the file name, line number, and column number.

StackFrame Debug . Debug , . , , , StackFrame.

0

StackTrace .

:

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"));
    }
}
0

stacktrace system.diagnostic - linqpad, .

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();
}
0

Source: https://habr.com/ru/post/1739825/


All Articles