473,659 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get Method Name

Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in void
method1() I will call void method5(). In method5 I want to know which
method called me? Because there are many methods calling many other methods
I really want a trace of code execution from method to method. Know what I
mean? any advice would be greatly appreciated.

Apr 2 '07 #1
9 26269
Hi,

Take a look at StackTrace & StackFrame classes

<msnews.microso ft.comwrote in message
news:em******** ******@TK2MSFTN GP04.phx.gbl...
Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many other
methods I really want a trace of code execution from method to method.
Know what I mean? any advice would be greatly appreciated.

Apr 2 '07 #2
msnews.microsof t.com wrote:
Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many
other methods I really want a trace of code execution from method to
method. Know what I mean? any advice would be greatly appreciated.
MethodInfo.GetC urrentMethod(). Name

and

(new StackTrace(true )).GetFrame(0). GetMethod().Nam e

are the two methods I know. I recommend the first: less code
and less runtime overhead.

Arne
Apr 3 '07 #3
Arne,
>msnews.microso ft.com wrote:
>Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many
other methods I really want a trace of code execution from method to
method. Know what I mean? any advice would be greatly appreciated.

MethodInfo.Get CurrentMethod() .Name

and

(new StackTrace(true )).GetFrame(0). GetMethod().Nam e

are the two methods I know. I recommend the first: less code
and less runtime overhead.
But that would return "method5" (the current method) when I believe
the original poster wants "method1" (the calling method).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 3 '07 #4
...
Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? ...
This blog helped me
http://blogs.msdn.com/jmstall/archiv...20/399287.aspx

A small test program that worked for me
-----------
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Diagnost ics;

namespace LineNumber
{
class LineNumber
{
public static void Foo()
{
StackTrace st = new StackTrace(true );
foreach (StackFrame f in st.GetFrames())
{
Console.WriteLi ne("On line {0}: '{1}'",
f.GetFileLineNu mber(),
f.GetMethod());
}
}

static void Main(string[] args)
{
StackTrace st = new StackTrace(true );
foreach (StackFrame f in st.GetFrames())
{
Console.WriteLi ne("On line {0}: '{1}'",
f.GetFileLineNu mber(),
f.GetMethod());
}

Console.WriteLi ne();
Foo();
}
}
}
-----------

Produced:
-----------
On line 24: 'Void Main(System.Str ing[])'

On line 13: 'Void Foo()'
On line 35: 'Void Main(System.Str ing[])'
-----------

Hope that helps,

Per

--

Per Erik Strandberg
..NET Architect - Optimization
Tomlab Optimization Inc.
http://tomopt.com/tomnet/
Apr 3 '07 #5
See also:
Finding the name of your calling method on Jeremy's blog:
http://jeremyjarrell.com/archive/2007/02/27/8.aspx
<msnews.microso ft.comwrote in message
news:em******** ******@TK2MSFTN GP04.phx.gbl...
Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many other
methods I really want a trace of code execution from method to method.
Know what I mean? any advice would be greatly appreciated.

Apr 3 '07 #6
Thanks everyone.

"D. Yates" <fo****@hotmail .comwrote in message
news:eZ******** ******@TK2MSFTN GP04.phx.gbl...
See also:
Finding the name of your calling method on Jeremy's blog:
http://jeremyjarrell.com/archive/2007/02/27/8.aspx
<msnews.microso ft.comwrote in message
news:em******** ******@TK2MSFTN GP04.phx.gbl...
>Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many
other methods I really want a trace of code execution from method to
method. Know what I mean? any advice would be greatly appreciated.

Apr 3 '07 #7
Mattias Sjögren wrote:
Arne,
>msnews.microso ft.com wrote:
>>Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many
other methods I really want a trace of code execution from method to
method. Know what I mean? any advice would be greatly appreciated.
MethodInfo.Get CurrentMethod() .Name

and

(new StackTrace(true )).GetFrame(0). GetMethod().Nam e

are the two methods I know. I recommend the first: less code
and less runtime overhead.

But that would return "method5" (the current method) when I believe
the original poster wants "method1" (the calling method).
I read:

"get the name of the method that the code is currently
executing in so that I can pass it to another method"

as being able to get the name of the method executing and passing that
as a string argument to another method.

void method1()
{
method5("method 1");
}

void method5(string metname)
{
}

But I can be wrong !

Arne
Apr 4 '07 #8
How is it possible to also get the parameters being passed?
<msnews.microso ft.comwrote in message
news:OV******** ******@TK2MSFTN GP05.phx.gbl...
Thanks everyone.

"D. Yates" <fo****@hotmail .comwrote in message
news:eZ******** ******@TK2MSFTN GP04.phx.gbl...
>See also:
Finding the name of your calling method on Jeremy's blog:
http://jeremyjarrell.com/archive/2007/02/27/8.aspx
<msnews.micros oft.comwrote in message
news:em******* *******@TK2MSFT NGP04.phx.gbl.. .
>>Is it possible to get the name of the method that the code is currently
executing in so that I can pass it to another method? For example, in
void method1() I will call void method5(). In method5 I want to know
which method called me? Because there are many methods calling many
other methods I really want a trace of code execution from method to
method. Know what I mean? any advice would be greatly appreciated.

Apr 4 '07 #9
Hi,

I'm not sure how to get the VALUE of the parameters being passed into the
method. You can get there name, position and type via ParameterInfo, but
I'm at a loss on how to get the value.

Also see the examples:
http://msdn2.microsoft.com/en-us/lib...me(vs.80).aspx
and
http://msdn2.microsoft.com/en-us/lib...ce(VS.80).aspx
Here's an example of getting the names of the parameters:
using System;
using System.Diagnost ics;
using System.Reflecti on;

namespace SamplePublic
{
// This console application illustrates various uses
// of the StackTrace and StackFrame classes.
class ConsoleApp
{
[STAThread]
static void Main()
{
Method1();

Console.WriteLi ne("Hit enter to exit.");
Console.ReadLin e();
}

private static void Method1()
{
Method2();
}

private static void Method2()
{
Method3();
}

private static void Method3()
{
Method4("Hello" , "World");
}

private static void Method4(string data1, string data2)
{
Method5();
}

private static void Method5()
{
StackTrace st = new StackTrace(true );
// Console.WriteLi ne(" Stack trace for this level: {0}",
st.ToString());

for (int i = 0; i < st.FrameCount; i++)
{
StackFrame sf = st.GetFrame(i);
Console.WriteLi ne(" File: {0}", sf.GetFileName( ));
Console.WriteLi ne(" Line Number: {0}", sf.GetFileLineN umber());
Console.WriteLi ne(" Column Number: {0}",
sf.GetFileColum nNumber());

MethodBase mb = sf.GetMethod();
Console.WriteLi ne("--Method called: " + mb.Name);
foreach (ParameterInfo pi in mb.GetParameter s())
{
Console.WriteLi ne("----Parameter Name: " + pi.Name);
Console.WriteLi ne("----Parameter Type: " + pi.ParameterTyp e);
Console.WriteLi ne("----Parameter Position: " + pi.Position);
}
}
}

}
}
Dave
Apr 5 '07 #10

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

Similar topics

1
1118
by: James | last post by:
We currently use Try/Catch blocks to handle exceptions, and when an exception occurs, we write it to the database with the method name and other information about the exception. We're currently changing the method name in every method and I'd like to get away from that. I found this: Dim st As New System.Diagnostics.StackTrace Dim sf As System.Diagnostics.StackFrame = st.GetFrame(0) Response.Write(sf.GetMethod().Name) ....which...
8
2660
by: AngryGerbil | last post by:
hey, How do I acquire MethodInfo WITHOUT hardcoding method name as a string?!??!The fact I have to use xxx.Gettype.GetMethod("MyMethod", xxx) is making me want to drive an ice pick into my eye! I have exhausted my resources and I cannot find a way out of it. Having to use that creates a dangerous situation because the name of the method could change. For instance, let's say you fav 3rd party library rolls out a new version and the...
5
2059
by: OneFang | last post by:
Hi I hope I make sense here. I want to be able to obtain the name of the method that calls a method within my class. So If I have my class that has a method LogInfo() And my client code has a method ProcessRequest()
3
2756
by: Merk | last post by:
How can I programmatically determine the from within that method. For example, consider the following code: private void DoSomething() { string s = ???; }
24
2098
by: Mike Hofer | last post by:
Please forgive the cross-post to multiple forums. I did it intentionally, but I *think* it was appropriate given the nature of my question. I'm working on an open source code library to help automate and clean up parameter validation code. It's almost ready to go into open beta. But one last little glitch is holding me up, and that would be the name of the factory class that serves as the entry point into the library: Validate.
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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
8627
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
7356
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
6179
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
5649
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
4175
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
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.