0

I'm wondering is there is a better or newer way to get the caller MethodInfo.

The only solution I have seen is using the StackFrame, though people mention that it's not always reliable in release builds. And it does not seem to work with async calls either :(

Alternative, I can use the CallerMemberNameAttribute, but that will only give me the method name as a string. It will be impossible to find any custom attributes on that method without knowing the class type (which is my intention).

Could I do something with the CallerFilePathAttribute?

Community
  • 1
  • 1
Ronald
  • 1,990
  • 6
  • 24
  • 39
  • Note that inlining means that the stack-frame approach isn't actually reliable, unless you explicitly disable inlining for both caller and callee - which is usually a bad thing to do. What is it that you actually want to do here? there may be better approaches. And `[CallerFilePath]` has very limited uses (it does have some though; we use it to inject the caller into our SQL as a comment, so it is easy to see what is causing SQL ops that show up on our performance dashboard. – Marc Gravell Nov 20 '14 at 11:06
  • I had some ideas that would require me to analyze the attributes defined on the caller method and perform specific actions on it. In theory, it would look very nice, but I guess that it's indeed not very practical :) – Ronald Nov 20 '14 at 11:14
  • 1
    another possibility is something like postsharp – Marc Gravell Nov 20 '14 at 11:31
  • Yes, but we don't use it :) That would have solved a lot – Ronald Nov 20 '14 at 11:39

0 Answers0