473,805 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Erro r 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 1293
Jim,
you can get the class name using:
Me.GetType().To String
(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.GetC urrentMethod()
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********@now here.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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("Erro r 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****@microso ft.com> wrote in message
news:Om******** ******@TK2MSFTN GP09.phx.gbl...
Jim,
you can get the class name using:
Me.GetType().To String
(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.GetC urrentMethod()
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
325
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 doing. I have a base class and will have several classes inheriting from it in the future.I need to in the constructor of the base class load an xml file that has the same name and the child class. since the base class constructor has no idea of...
6
3719
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 instance. // Declare the types. string AssemblyToRun = "C:\MyAss"; string ClassToRun = "MyClass" string MethodToRun = "MyMethod"
0
8452
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. xxx======================================================================
8
1650
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
2160
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 that I use for acquiring the function names, scope, parameters, etc. Is there an "InheritedClassType" or "InterfaceType" property that I can query? I've tried using DeclaringType.BaseType, but that doesn't seem to give me what I want. ...
13
12395
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
4583
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 constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
2
2600
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 property. The overall goal is to be able to reverse engineer a given assembly and create a diagram of dependencies. For instance, let's say that method foo() of class bob makes a call to method bar() of class sally, I would like to be able to...
4
6815
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 signature of method to pass a new parameter, I am setting the custom attribute of a given method and then accessing the attribute from method. Since attribute value is decided at runtime I want to change the attribute of particular method at runtime....
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.