Posted By

rengber on 01/14/10


Tagged

logging Reflection


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


Get the Namespace, Class Name, and Method Name of the Currently Executing Method Via Reflection


 / Published in: C#
 

GetFrame(1) to avoid the last method being this property accessor. Note that in a child class inherited member, the Class name will be the name of the type containing the implementation.

  1. protected string Source
  2. {
  3. get
  4. {
  5. StackTrace st = new StackTrace(true);
  6. MethodBase mb = st.GetFrame(1).GetMethod();
  7. return mb.ReflectedType.Namespace + "." + mb.ReflectedType.Name + "." + mb.Name;
  8. }
  9. }
  10.  
  11. //Use like so:
  12. public void DoChildStuff()
  13. {
  14. Log(Source, "Doing Child Stuff");
  15. }

Report this snippet  

You need to login to post a comment.