473,503 Members | 2,167 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 26262
Hi,

Take a look at StackTrace & StackFrame classes

<msnews.microsoft.comwrote in message
news:em**************@TK2MSFTNGP04.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.microsoft.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.GetCurrentMethod().Name

and

(new StackTrace(true)).GetFrame(0).GetMethod().Name

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

Arne
Apr 3 '07 #3
Arne,
>msnews.microsoft.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.GetCurrentMethod().Name

and

(new StackTrace(true)).GetFrame(0).GetMethod().Name

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.Collections.Generic;
using System.Text;
using System.Diagnostics;

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

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

Console.WriteLine();
Foo();
}
}
}
-----------

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

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

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.microsoft.comwrote in message
news:em**************@TK2MSFTNGP04.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**************@TK2MSFTNGP04.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.microsoft.comwrote in message
news:em**************@TK2MSFTNGP04.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.microsoft.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.GetCurrentMethod().Name

and

(new StackTrace(true)).GetFrame(0).GetMethod().Name

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("method1");
}

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.microsoft.comwrote in message
news:OV**************@TK2MSFTNGP05.phx.gbl...
Thanks everyone.

"D. Yates" <fo****@hotmail.comwrote in message
news:eZ**************@TK2MSFTNGP04.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.microsoft.comwrote in message
news:em**************@TK2MSFTNGP04.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.Diagnostics;
using System.Reflection;

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

Console.WriteLine("Hit enter to exit.");
Console.ReadLine();
}

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.WriteLine(" Stack trace for this level: {0}",
st.ToString());

for (int i = 0; i < st.FrameCount; i++)
{
StackFrame sf = st.GetFrame(i);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
Console.WriteLine(" Column Number: {0}",
sf.GetFileColumnNumber());

MethodBase mb = sf.GetMethod();
Console.WriteLine("--Method called: " + mb.Name);
foreach (ParameterInfo pi in mb.GetParameters())
{
Console.WriteLine("----Parameter Name: " + pi.Name);
Console.WriteLine("----Parameter Type: " + pi.ParameterType);
Console.WriteLine("----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
1112
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...
8
2656
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...
5
2052
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...
3
2744
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
2080
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...
0
7205
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,...
0
7287
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,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
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...
0
4689
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...
0
3180
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...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
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...

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.