trace declaration name

  • Thread starter Thread starter Mark Oliver
  • Start date Start date
M

Mark Oliver

Hi,

Is there any way to get the declaration name of a parameter either
through reflection, StackFrame, or something else?

Thanks,
Mark
 
Hi,

Is there any way to get the declaration name of a parameter
either
through reflection, StackFrame, or something else?

Mark,

using System.Reflection;
...
public static void Main(string[] args)
{
Type t = typeof(Example1);
MethodBase mb = t.GetMethod("Main");
ParameterInfo[] pi = mb.GetParameters();

foreach (ParameterInfo p in pi)
Console.WriteLine("Param #" + p.Position + "'s name is " +
p.Name);
}

This will print:

Param #0's name is args


Hope this helps.

Chris.
 
Hi,

Thanks for the info. Here is an example of what I am looking for:

public class LogParameterName
{
public LogParameterName(int x)
{
// I want to see something like this:
// parameter "x" declared as "i"
}
public LogParameterName(Ugly ugly)
{
ConstructorInfo ci=this.GetType().GetConstructor(new
Type[]{ugly.GetType()});

Console.WriteLine("parameter \"{0}\" declared as
\"{1}\"",ci.GetParameters()[0].Name,ugly.name);
}
}
public class Ugly
{
public string name;
public Ugly(string name)
{
this.name=name;
}
}
public class test
{
int i=0;
public test()
{
new LogParameterName(i);
// this would look a lot nicer and less prone to typeos if I
could use C++ style macros
Ugly testUgly=new Ugly("testUgly");
new LogParameterName(testUgly);
}
}




Chris R. Timmons said:
Hi,

Is there any way to get the declaration name of a parameter
either
through reflection, StackFrame, or something else?

Mark,

using System.Reflection;
...
public static void Main(string[] args)
{
Type t = typeof(Example1);
MethodBase mb = t.GetMethod("Main");
ParameterInfo[] pi = mb.GetParameters();

foreach (ParameterInfo p in pi)
Console.WriteLine("Param #" + p.Position + "'s name is " +
p.Name);
}

This will print:

Param #0's name is args


Hope this helps.

Chris.
 
I wrote some code to log the file name,line,column, and text of the caller
from StackFrame, good enough for me.

Mark Oliver said:
Hi,

Thanks for the info. Here is an example of what I am looking for:

public class LogParameterName
{
public LogParameterName(int x)
{
// I want to see something like this:
// parameter "x" declared as "i"
}
public LogParameterName(Ugly ugly)
{
ConstructorInfo ci=this.GetType().GetConstructor(new
Type[]{ugly.GetType()});

Console.WriteLine("parameter \"{0}\" declared as
\"{1}\"",ci.GetParameters()[0].Name,ugly.name);
}
}
public class Ugly
{
public string name;
public Ugly(string name)
{
this.name=name;
}
}
public class test
{
int i=0;
public test()
{
new LogParameterName(i);
// this would look a lot nicer and less prone to typeos if I
could use C++ style macros
Ugly testUgly=new Ugly("testUgly");
new LogParameterName(testUgly);
}
}




Chris R. Timmons said:
Hi,

Is there any way to get the declaration name of a parameter
either
through reflection, StackFrame, or something else?

Mark,

using System.Reflection;
...
public static void Main(string[] args)
{
Type t = typeof(Example1);
MethodBase mb = t.GetMethod("Main");
ParameterInfo[] pi = mb.GetParameters();

foreach (ParameterInfo p in pi)
Console.WriteLine("Param #" + p.Position + "'s name is " +
p.Name);
}

This will print:

Param #0's name is args


Hope this helps.

Chris.
 
Back
Top