발신자 기능을 어떻게 찾습니까? [복제하다
-
07-07-2019 - |
문제
정확한 복제로 닫혔습니다 "현재 방법이라고 불리는 방법을 어떻게 찾을 수 있습니까?"
~이다 이것 C#로 가능합니까?
void main()
{
Hello();
}
void Hello()
{
// how do you find out the caller is function 'main'?
}
해결책
Console.WriteLine(new StackFrame(1).GetMethod().Name);
그러나 특히 최적화 (예 : Jit Inlining)가 인식 된 스택 프레임으로 원숭이를 만들 수 있기 때문에 이것은 강력하지 않습니다.
다른 팁
에서 여기:
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1);
System.Diagnostics.StackFrame sf = st.GetFrame(0);
string msg = sf.GetMethod().DeclaringType.FullName + "." +
sf.GetMethod().Name;
MessageBox.Show( msg );
그러나 이것이 멀티 스레딩에서 작동 할 수 없다는 말도 있습니다.
제휴하지 않습니다 StackOverflow