2

Is there a way to get all caller of C# method ie:

public string Caller(string str)
{      
  Customer cust = new Customer();
  cust.Firstname = "Peter";
  cust.LastName = "Beamer";
  string t = getName(cust);
  return t;
}

private string getName(Customer customer)
{
  return customer.Firstname +" "+ customer.LastName;
}

would return: Caller.

All I can get now is method body text using EnvDTE.CodeFunction. Maybe there is a better way to achieve it than trying to parse this code.

Note: I don't want to get current method's calling method name. I want that If I give name of the method then It will return passed method's calling methods name.

ChandniShah
  • 249
  • 1
  • 3
  • 12
  • 6
    *What* would return `Caller`? It's not clear what your context is here. – Jon Skeet Jul 09 '13 at 12:59
  • 2
    possible duplicate of [How can I find the method that called the current method?](http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method) – Gray Jul 09 '13 at 12:59
  • @Gray : I don't want to get current method's caller method name. I want that If I give method name than my code should return passed method's caller. In my given example, If I give 'getName' as method name then my code should return 'Caller' . – ChandniShah Jul 10 '13 at 04:14
  • @ChandniShah why are you passing `Customer` to `getName` if what you want it to return is "Caller"? Try `MethodBase.GetCurrentMethod()`, instead of `getName` in `Caller` function. – nawfal Jan 12 '14 at 22:27

3 Answers3

6
new StackFrame(1, true).GetMethod().Name

Note that in release builds the compiler might inline the method being called, in which case the above code would return the caller of the caller, so to be safe you should decorate your method with:

[MethodImpl(MethodImplOptions.NoInlining)]

source: https://stackoverflow.com/a/1310148/1714342

Community
  • 1
  • 1
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • 4
    Just beware of the performance impact. Do not use this in production code. If you think you actually need it, take a look at PostSharp – Steve B Jul 09 '13 at 13:00
  • @SteveB, I was going to say the exact same thing! – Mike Perrenoud Jul 09 '13 at 13:02
  • @wudzik : This will return current method's caller name. I want that If I give method name than my code should return passed method's caller. – ChandniShah Jul 10 '13 at 04:11
  • @ChandniShah so you want to new StackFrame(2,true).GetMethod().Name? – Kamil Budziewski Jul 10 '13 at 05:21
  • @wudzik: I don't think it will work. I am developing visual studio plugin from which I am analyzing the code of project. So from plugin code if I pass name of some class from my host project. it should give me caller methods name. – ChandniShah Jul 10 '13 at 05:36
  • @ChandniShah now I get it, it's more complicated then :D this topic is more related to your task http://stackoverflow.com/questions/5490025/c-sharp-reflection-and-find-all-references – Kamil Budziewski Jul 10 '13 at 05:40
5

Not really positive I understand what you're asking since no one seems sure what should return "Caller"... but, perhaps the CallerMemberNameAttribute might be of some help?

Tim
  • 14,999
  • 1
  • 45
  • 68
4

While it is certainly possible to do it using StackFrame as wudzik points out, why would you need to do this?

This is a design smell. If your code has to have knowledge about its caller, something is wrong with your design. Every such dependency should be abstracted out of your class model.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • I think it could potentially be helpful for debugging, but I agree that it very much seems like something that your production code should not use or depend on. – Gray Jul 09 '13 at 13:09
  • 1
    If you put a breakpoint in code, you can examine the entire stack trace at any time. It would be unnecessary to put such dependencies in code, even for debugging purposes... The code would quickly become unreadable and unmaintainable (very hard to change!) – Roy Dictus Jul 09 '13 at 13:11
  • That makes sense; I don't think it belongs in code for the long term in anyway. It definitely seems like something you would do in a language that does not include the powerful debugger that comes with Visual Studio. It just sort of has the feel of a temporary 'Debug.WriteLine' that you throw in to help you understand the flow better. More of an educational/novice tool for someone not yet comfortable with the debugger. There may be some truly legitimate use with profiling/optimizing, but I can't come up with a great example. – Gray Jul 09 '13 at 13:31
  • @RoyDictus: my production code is not dependent on this. I am developing one visual studio plugin in which I have to parse the code. – ChandniShah Jul 10 '13 at 03:53