trace declaration name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Oliver

    #1

    trace declaration name

    Hi,

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

    Thanks,
    Mark


  • Chris R. Timmons

    #2
    Re: trace declaration name

    "Mark Oliver" <mark_oliver@co mcast.net> wrote in
    news:ovAjb.4819 33$2x.196363@rw crnsc52.ops.asp .att.net:
    [color=blue]
    > Hi,
    >
    > Is there any way to get the declaration name of a parameter
    > either
    > through reflection, StackFrame, or something else?[/color]

    Mark,

    using System.Reflecti on;
    ...
    public static void Main(string[] args)
    {
    Type t = typeof(Example1 );
    MethodBase mb = t.GetMethod("Ma in");
    ParameterInfo[] pi = mb.GetParameter s();

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

    This will print:

    Param #0's name is args


    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.

    Comment

    • Mark Oliver

      #3
      Re: trace declaration name

      Hi,

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

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

      Console.WriteLi ne("parameter \"{0}\" declared as
      \"{1}\"",ci.Get Parameters()[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 LogParameterNam e(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 LogParameterNam e(testUgly);
      }
      }




      "Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote in message
      news:Xns9416970 57A54Acrtimmons crtimmonsin@207 .46.248.16...[color=blue]
      > "Mark Oliver" <mark_oliver@co mcast.net> wrote in
      > news:ovAjb.4819 33$2x.196363@rw crnsc52.ops.asp .att.net:
      >[color=green]
      > > Hi,
      > >
      > > Is there any way to get the declaration name of a parameter
      > > either
      > > through reflection, StackFrame, or something else?[/color]
      >
      > Mark,
      >
      > using System.Reflecti on;
      > ...
      > public static void Main(string[] args)
      > {
      > Type t = typeof(Example1 );
      > MethodBase mb = t.GetMethod("Ma in");
      > ParameterInfo[] pi = mb.GetParameter s();
      >
      > foreach (ParameterInfo p in pi)
      > Console.WriteLi ne("Param #" + p.Position + "'s name is " +
      > p.Name);
      > }
      >
      > This will print:
      >
      > Param #0's name is args
      >
      >
      > Hope this helps.
      >
      > Chris.
      > -------------
      > C.R. Timmons Consulting, Inc.
      > http://www.crtimmonsinc.com/[/color]


      Comment

      • Mark Oliver

        #4
        Re: trace declaration name

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

        "Mark Oliver" <mark_oliver@co mcast.net> wrote in message
        news:o_Xjb.5771 32$Oz4.552786@r wcrnsc54...[color=blue]
        > Hi,
        >
        > Thanks for the info. Here is an example of what I am looking for:
        >
        > public class LogParameterNam e
        > {
        > public LogParameterNam e(int x)
        > {
        > // I want to see something like this:
        > // parameter "x" declared as "i"
        > }
        > public LogParameterNam e(Ugly ugly)
        > {
        > ConstructorInfo ci=this.GetType ().GetConstruct or(new
        > Type[]{ugly.GetType() });
        >
        > Console.WriteLi ne("parameter \"{0}\" declared as
        > \"{1}\"",ci.Get Parameters()[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 LogParameterNam e(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 LogParameterNam e(testUgly);
        > }
        > }
        >
        >
        >
        >
        > "Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote in message
        > news:Xns9416970 57A54Acrtimmons crtimmonsin@207 .46.248.16...[color=green]
        > > "Mark Oliver" <mark_oliver@co mcast.net> wrote in
        > > news:ovAjb.4819 33$2x.196363@rw crnsc52.ops.asp .att.net:
        > >[color=darkred]
        > > > Hi,
        > > >
        > > > Is there any way to get the declaration name of a parameter
        > > > either
        > > > through reflection, StackFrame, or something else?[/color]
        > >
        > > Mark,
        > >
        > > using System.Reflecti on;
        > > ...
        > > public static void Main(string[] args)
        > > {
        > > Type t = typeof(Example1 );
        > > MethodBase mb = t.GetMethod("Ma in");
        > > ParameterInfo[] pi = mb.GetParameter s();
        > >
        > > foreach (ParameterInfo p in pi)
        > > Console.WriteLi ne("Param #" + p.Position + "'s name is " +
        > > p.Name);
        > > }
        > >
        > > This will print:
        > >
        > > Param #0's name is args
        > >
        > >
        > > Hope this helps.
        > >
        > > Chris.
        > > -------------
        > > C.R. Timmons Consulting, Inc.
        > > http://www.crtimmonsinc.com/[/color]
        >
        >[/color]


        Comment

        Working...