I want to recall a method from the called method, can i use action for that, or any other solution.
-
1Be careful! You might be asking for more troubles than you want! What is your use case? – Moon Nov 17 '15 at 05:19
-
I think the OP means: A() calls B() and in B() he wants to call A() again. – Sweeper Nov 17 '15 at 05:21
-
1Your question is unclear. Please edit your question. – Oguz Ozgul Nov 17 '15 at 05:21
-
Does he know what A is while in B – Spaceman Nov 17 '15 at 05:21
-
The only solution in A and B case would be to create a new StackTrace() in B and to call the method at frame 1 I think then restart the application because it will crash with stack overflow exception – Oguz Ozgul Nov 17 '15 at 05:22
-
`I want to recall a method from the called method` No you don't. Your design is wrong and needs to be fixed. – John3136 Nov 17 '15 at 05:24
-
function pointers can help i guess? – null1941 Nov 17 '15 at 05:25
-
It seems you are looking for [CallBack](http://stackoverflow.com/questions/2139812/what-is-a-callback) mechanism? – Siva Gopal Nov 17 '15 at 05:27
-
But callback should also be meaning full. Tell me this, 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?! – Siva Gopal Nov 17 '15 at 05:31
-
The situation is bit complicated, dont know wheather its possible to it or not. i want to generalise a function in a method, that method should call the function once again. – Sonu Jose Nov 17 '15 at 05:42
-
1Please provide at least pseudo code of what you are trying to achieve – Oguz Ozgul Nov 17 '15 at 05:44
-
Actually i want to add retry prompt based on the input by user, if user says to retry then i need to call the function once again, but the code cannot be duplicated , as it is used in many cases – Sonu Jose Nov 17 '15 at 05:45
2 Answers
You should not do this in normal situations, your design is probably wrong!
You can use the CallerInfoAttribute
to get the caller and use reflection to invoke it.
But... that is a lot of work and I think you are not very skilled. Luckily, there is another way!
You can add a parameter of type MethodInfo
. And let's say A()
calls B(MethodInfo)
. Then A
needs to get its reflected self and pass it into B
. Then in B
you can use MethodInfo.Invoke
to invoke the method.
Well... it turns out that the alternative requires reflection too... So if you are not familiar with reflection, learn more about it first!
If you want to know more about MethodInfo
, go to here: https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo(v=vs.110).aspx

- 213,210
- 22
- 193
- 313
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..

- 1
- 1

- 3,474
- 1
- 25
- 22