473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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


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
2 1269
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: kilian koltari | last post by:
I need to determine what method called a current method. I believe i need to use reflection to retrieve the calling methods name but can't find info on it. any pointers? Details of what i am...
6
by: kapilp | last post by:
hi, i have a program that used reflection to execute methods. now i want to execute the reflected method on a new thread but cant figure out how or if it can be done. take the below code, for...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
8
by: paccala | last post by:
Ciao Being google-ing since hours and still I'm not able to solve my problem... I have following hierarchy: class A { public void myMethod(); }
2
by: Rob | last post by:
I'm using reflection to inspect a class and its methods. Is is possible to determine if the class is a derived class or an interface implementation? I'm using the MethodInfo object for relfection...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
2
by: Nate | last post by:
All, I've been searching all over Google for a nice code sample explaining how to use reflection to get the instance of a method or property and then retrieve just the IL code for that method or...
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.