Using reflection to determine the class & method I'm in?

  • Thread starter Thread starter Jim Bancroft
  • Start date Start date
J

Jim Bancroft

Hi all,

I'm writing an exception handler for one of my VB.Net methods and
wondered how best to dynamically put the class and method name in my message
string.

My code looks like this currently:


Catch ex as Exception
Dim ex1 As New Exception("Error encountered in
Class-- MyClass, Method-- MyMethod", ex)
Throw ex1


Obviously, hardcoding the class and method names isn't such a hot
idea. I assume reflection will help pull the right information, however I'm
new to that subject and wondered if someone could help provide a code
snippet for this, or a link to where I can learn more? Thanks!

-Jim
 
Jim,
you can get the class name using:
Me.GetType().ToString
(ofcourse, assuming you are calling this from an instance method; in a
shared method, Me would be nothing so you won't be able to do the above)

to get the currently executing method, you can do:
MethodBase.GetCurrentMethod()
this returns a MethodBase object from which you can then retrieve the name,
parameters, etc of the method. I haven't tried this one but I think this
should work.

hope this helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top