4

Is there a trick in System.Reflection to get the Namespace of a method that is calling another method? What I currently have is:

var stackTrace = new System.Diagnostics.StackTrace();
MethodBase method = stackTrace.GetFrame(1).GetMethod();
string namespace = method.DeclaringType.Namespace;
Assembly assembly = method.DeclaringType.Assembly;
Damith
  • 62,401
  • 13
  • 102
  • 153
koruptid
  • 41
  • 2
  • 3
  • Duplicate - http://stackoverflow.com/questions/615940/retrieving-the-calling-method-name-from-within-a-method-c – eyossi Jun 18 '12 at 20:08
  • 1
    Watch out for optimization in the Release configuration, it can change the call stack. – ashes999 Jun 18 '12 at 20:08
  • 2
    You can use the [System.Runtime.CompilerServices.MethodImpl( System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] so in release it won't inline the method – eyossi Jun 18 '12 at 20:12

1 Answers1

0

No, and even your trick will NOT WORK in production if the calling method is inlined - i.e. so small that the compiler makes it in line to the next one.

There is no reliable way to get this infomration.

http://www.mehdi-khalili.com/that-tricky-stacktrace

TomTom
  • 61,059
  • 10
  • 88
  • 148