How to get the current class

G

gman

How can you get the current class of a method?

We are writting out trace error messages using the
System.Reflection.MethodBase.GetCurrentMethod to get the Current
Method but a string is passed telling for the class. I am a newbie to
the .NET but I would think there should be a way to say
GetCurrentClass?

Here is our code.
==========================
Imports System.Reflection.MethodBase
....
Public Class ctlUserMaintenance : Inherits ctlBaseEdit
....
Protected Overrides Sub InitForm()
mPageName = "User Maintenance"
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Try
SetDirtyFlag(False)
InitGrid()
Catch ex As Exception
SendError(ex, GetCurrentMethod.Name, mPageName)
End Try
End Sub
==========================

The SendError routine displays/logs the error. As you can see
GetCurrentMethod.Name is used to pass "OnLoad" but mPageName is hard
coded with "User Maintenance". I would like a way to get the Class
name of "ctlUserMaintenance" without having to hard code it. Then I
would be able to tell exactly what class thrown the error.

Any other ideas on how to handle error conditions would be appretiated
too.
It would be really cool if I could get the SendError to open to run
"DevEnv /Edit" and have it open the class that is causing the problem
so I can debug.

Thanks in advance.
 
M

Mattias Sjögren

How can you get the current class of a method?

In an instance method such as OnLoad you can simply check
Me.GetType().Name. In a Shared method,
MethodBase.GetCurrentMethod().DeclaringType.Name.



Mattias
 
C

Chris Mullins [MVP - C#]

The most obvious answer, "me.GetType" is probably what you're looking for.

This may (or may not) give the right polymorphic answer, depending on what
you want. If you're in a base class, you'll end up getting the type of the
derived class. Sometims this is what you're looking for, sometimes not.

.... also, this won't work inside a shared method, as there's no "me"
parameter available.
 

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

Top