Get Method Name

G

Guest

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

msnews.microsoft.com said:
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
 
M

Mattias Sjögren

Arne,
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
 
P

per9000

...
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/archive/2005/03/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/
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mattias said:
Arne,


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
 
D

D. Yates

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/library/system.diagnostics.stackframe(vs.80).aspx
and
http://msdn2.microsoft.com/en-us/library/system.diagnostics.stacktrace(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
 

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