By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,975 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,975 IT Pros & Developers. It's quick & easy.

Getting Methodbase and all parameters passed to that method

P: n/a
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);

for (int index = 0; index < trace.FrameCount; ++index)
{
StackFrame frame = trace.GetFrame(index);
MethodBase method = frame.GetMethod();

string methodName = method.Name;
ParameterInfo[ ] methodParameters = method.GetParameters();
break;
}
}

I can get the type and names of those parameters. But the issue is to
get the values of those parameters. Does anyone know how to do that?

Mar 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
As per my understanding, you cannot get runtime values using StackFrame or
Reflection.

Here is a sample code that shows how can you retrieve runtime parameter
values using param object[]. Most often you would like to use it for logging
purposes.

Thanks, Arif
////////////////////Sample code/////////////////////////////////
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Sample One
int val = 10;
string name = "Arif";
BusinessFunction1(val,name);

//Sampe Two
int b = 10;
string name2 ="Test";
bool check = true;
BusinessFunction2(b,name2,check);
}

static void BusinessFunction1(int val, string name)
{
LogTrace(val,name);
}
static void BusinessFunction2(int b, string name2, bool check)
{
LogTrace(b,name2,check);
}

public static void LogTrace(params object[] args)
{
StackTrace callStack = new StackTrace(1, true);
StackFrame callingMethodFrame = callStack.GetFrame(0);

if (args == null || args.Length == 0)
{
Console.WriteLine("no parameters");
}
else
{
MethodBase callingMethod = callingMethodFrame.GetMethod();
ParameterInfo[] parameters = callingMethod.GetParameters();

int inParamCount = parameters.Length;
foreach (ParameterInfo parameter in parameters)
if (parameter.IsOut) inParamCount--;

Debug.Assert(inParamCount == args.Length, String.Format("Incorrect
number of arguments. Expected {0} but was {1}.", parameters.Length,
args.Length));

int paramCount = parameters.Length;
int argIndex = 0;
for (int i = 0; i < paramCount; i++)
{
Console.WriteLine(parameters[i].Name);
if (parameters[i].IsOut)
Console.WriteLine("<Unknown>");
else
{
if (args[argIndex] == null)
Console.WriteLine("<null>");
else
{
Console.WriteLine(args[argIndex].ToString().Trim());
}
argIndex++;
}
if (i < paramCount - 1)
Console.WriteLine(",");
}
}
}
}
"kp******@gmail.com" wrote:
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);

for (int index = 0; index < trace.FrameCount; ++index)
{
StackFrame frame = trace.GetFrame(index);
MethodBase method = frame.GetMethod();

string methodName = method.Name;
ParameterInfo[ ] methodParameters = method.GetParameters();
break;
}
}

I can get the type and names of those parameters. But the issue is to
get the values of those parameters. Does anyone know how to do that?

Mar 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.