473,378 Members | 1,470 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,378 software developers and data experts.

Getting Class and Method name at runtime

Is there a way to get the name of the class and method in code when not
running in debugger.

For example:
class myclass {
const string modulename = "myclass";

public void mymethod()
{
const string methodname = "mymethod";

string output = "You are in " + modulename + "." + methodname;
}
}

Is there a way to determine the class and method without having to hard code
them?

I use this functionality when reporting messages to the database so that I
can determine where something occurred.

Thank you for any help.

Gregory McCallum
gm*******@honovi.com

Nov 17 '05 #1
5 2756
gmccallum wrote:
Is there a way to get the name of the class and method in code when not
running in debugger.


See MethodBase.GetCurrentMethod. You may want to experiment with
exactly which class you want to log, eg if you're in a method declared
in A, overridden in B, and your actual instance is an instance of C...

Jon

Nov 17 '05 #2
Perfect, thank you.

"Jon Skeet [C# MVP]" wrote:
gmccallum wrote:
Is there a way to get the name of the class and method in code when not
running in debugger.


See MethodBase.GetCurrentMethod. You may want to experiment with
exactly which class you want to log, eg if you're in a method declared
in A, overridden in B, and your actual instance is an instance of C...

Jon

Nov 17 '05 #3
On the same topic is it possible to get the class and method that
CALLED the current class and method?

"Jon Skeet [C# MVP]" wrote:
gmccallum wrote:
Is there a way to get the name of the class and method in code when not
running in debugger.


See MethodBase.GetCurrentMethod. You may want to experiment with
exactly which class you want to log, eg if you're in a method declared
in A, overridden in B, and your actual instance is an instance of C...

Jon

Nov 17 '05 #4
I found the answer, so I thought I would share it with others.
This function will return the name of the class and method of the calling
function.

using System.Diagnostics;

private static string[] internGetModuleAndProc ( int stackFramesToSkip )
{
string[] ret = new string[2];
if ( stackFramesToSkip<0 )
stackFramesToSkip=0;
// Skipping 1 to leave out the call to this method
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace (
1+stackFramesToSkip, true );
System.Diagnostics.StackFrame[] sf = st.GetFrames ();
MethodBase mb = sf[0].GetMethod ();
ret[0] = mb.DeclaringType.Name;
ret[1] = mb.Name;
return ret;
}

public void Display_Now ( )
{
// This would set s5 = "THISCLASS.Display_Now"
string s5 = General.GetModuleAndProc ( 0 );
}

Nov 17 '05 #5
On Fri, 28 Oct 2005 11:06:07 -0700, gmccallum
<gm*******@discussions.microsoft.com> wrote:
I found the answer, so I thought I would share it with others.
This function will return the name of the class and method of the calling
function.

public void Display_Now ( )
{
// This would set s5 = "THISCLASS.Display_Now"
string s5 = General.GetModuleAndProc ( 0 );
}


Be aware that this solution has a limitation. If the Display_Now
method gets inlined, the string s5 will contain the parent method and
not Display_Now.

The following code should print False if run in the debugger and True
if run outside of it.

public static bool Test()
{
return internGetModuleAndProc(0)[1] == "Main";
}

static void Main()
{
Console.WriteLine("Inlined: " + Test().ToString());
Console.ReadLine();
}

--
Marcus Andrén
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
6
by: Baris | last post by:
Given the C# code below, can anyone think of a better class design? What really gets my goat is that the code within derived classes D1 and D2 is identical and in my mind should be refactored into...
2
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be...
2
by: Larry | last post by:
I'm having trouble finding the right method to determine at runtime the data type of the fields in a class. here's what I've got so far: Public Class ImgSubmissionRecord Public fileName As...
8
by: ABC | last post by:
How to return the class name using static getter from a class? The base class as: class abcbase { .............. public static string ObjectName {
1
by: kplkumar | last post by:
I am trying to get the method and the parameters passed to that method from the stackTrace. My code is, private void GetExecutingMethodInfo() { StackTrace trace = new StackTrace(false); ...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.