I see some ambiguity in your question use case..
Let us take a simple use case: You want to call some one and they in turn call you back. Is there any use to it and when do you want to stop this and go ahead to get some work done?!
For example following code like what you are expecting will go in infinite recursion and result in StackOverflowException
class MyClass
{
public void TargetMethod(Action callback)
{
Console.WriteLine("Inside TargetMethod");
callback(); //This call the SourceMethod() and which inturn call TargetMethod() again in infinite recursion.
}
public void SourceMethod()
{
Console.WriteLine("Inside SourceMethod");
TargetMethod(SourceMethod);
}
}
//Calling code
MyClass objMyClass = new MyClass();
objMyClass.SourceMethod();
Instead you can use Callback mechanism, so that once you done within the target method, notify another handler method that in turn can do some stuff like logging or updating UI etc., case specific stuff.
class MyClass
{
public void TargetMethod(Action callback)
{
Console.WriteLine("Inside TargetMethod");
callback(); //This calls/notify the handler to do some stuff.
}
public void SourceMethod()
{
Console.WriteLine("Inside SourceMethod");
TargetMethod(CallbackHandler); //Notice here we are calling to a handler which can do some stuff for us.
}
public void CallbackHandler()
{
Console.WriteLine("Inside CallbackHandler");
}
}
This code is just for quick demonstration purpose and you can enhance design further in your implementation.
Hope this provide you some heads-up on why you need to revisit your design..
3
solved How to invoke a method that called the current method in c# [closed]