By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,238 Members | 1,532 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,238 IT Pros & Developers. It's quick & easy.

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

P: n/a

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


Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Jim Bancroft" <bo********@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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

Nov 21 '05 #2

P: n/a
Imran, thanks a lot-- this is just what I needed.

-Jim

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.