Anyone know if ClassName & MethodName are accessible in .NET code?

  • Thread starter Thread starter Steve Hiemstra
  • Start date Start date
S

Steve Hiemstra

Hi All,

I want to be able to put the ClassName and MethodName into my Exception
Handler messages from code, but I don't want to 'hardcode' the names (so I
can cut&paste my handler routines more easily).

Anyone find some 'macros' or other .NET methods/properties that already do
this?

SteveH
 
Steve said:
Hi All,

I want to be able to put the ClassName and MethodName into my Exception
Handler messages from code, but I don't want to 'hardcode' the names (so I
can cut&paste my handler routines more easily).

Anyone find some 'macros' or other .NET methods/properties that already do
this?

This will get you the currently executing method name.

Dim st As New Stacktrace
Dim sf As StackFrame = st.GetFrame(0)
MessageBox.Show(sf.GetMethod.Name)
 
Steve Hiemstra said:
Hi All,

I want to be able to put the ClassName and MethodName into my Exception
Handler messages from code, but I don't want to 'hardcode' the names (so I
can cut&paste my handler routines more easily).

Anyone find some 'macros' or other .NET methods/properties that already do
this?

If you print the stack trace, aren't the class and method names there?

try
{
do something;
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
 
Yes, the Class & Method name are in the Exception Message by default.
You are right. I guess I wasn't wording my question appropriately...

Sometimes during an Exception you want to 'write' out the ClassName and
MethodName to the user, or console (not just to the Exception Handler).
I want to have 'control' at this level.

Still, your answer is correct.

Thanks for pointing this out,

SteveH
 
Interesting. I have never wanted to write the class name and method name
(and signature) independantly of displaying the stack trace.

Good luck, and be sure that your technique deals with overloaded methods,
overridden methods, operators, etc. It should also deal with generics in the
Whidbey timeframe.
 
Back
Top