开发者

Find function Invoker

开发者 https://www.devze.com 2023-03-16 06:16 出处:网络
I have C# Function like this : private void test(){} Is there any way to find Invoker or caller of a function ?

I have C# Function like this :

private void test(){}

Is there any way to find Invoker or caller of a function ?

For example :

private void Caller(){
test();
}

then how can I understand that test was inv开发者_运维百科oked by Caller ?


See how to get invoker class of this method


You can get the names of the methods from call stack.

When you call a function, the name also as the other info is pushed to call stack so the program knows how to return from that function call. You can use that info to obtain the previous function that called your test() function.

To get stacktrace you can use the following:

StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames();

Console.WriteLine(stackFrames[1].GetMethod().Name);    


use a parameter in the caller that will be different upon the method call the test.like when you call test from caller use test(1) when you call from another function say from anotherCaller use test(2)


You can a string argument to to test,

private void test(string whoCalled){}

If and in Caller,

private void Caller() {
    test("Caller");
}
0

精彩评论

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