Getting values of parameters to method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Quinn

    Getting values of parameters to method

    Is there a way to get the values of the paramaters to a
    method programatically ?

    I know that I can use reflection to find out the
    parameter names and types, etc., but I want to know the
    values of those parameters too.

    I'm trying to write a generic error handler. I wan't to
    be able to log the values that caused the error, without
    the user of the class having to specify the values 'cause
    they're lazy ;)

    Thanks,
    Brad
  • 100

    #2
    Re: Getting values of parameters to method

    Hi Brad,
    Parameters have value only inside the body of the function (at run time).
    Reflection won't help here.

    B\rgds
    100

    "Brad Quinn" <brad_quinn@yah oo.com> wrote in message
    news:013001c3a9 4a$e45f22e0$a40 1280a@phx.gbl.. .[color=blue]
    > Is there a way to get the values of the paramaters to a
    > method programatically ?
    >
    > I know that I can use reflection to find out the
    > parameter names and types, etc., but I want to know the
    > values of those parameters too.
    >
    > I'm trying to write a generic error handler. I wan't to
    > be able to log the values that caused the error, without
    > the user of the class having to specify the values 'cause
    > they're lazy ;)
    >
    > Thanks,
    > Brad[/color]


    Comment

    • Roy Osherove

      #3
      Re: Getting values of parameters to method

      On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:
      [color=blue]
      > Is there a way to get the values of the paramaters to a
      > method programatically ?
      >
      > I know that I can use reflection to find out the
      > parameter names and types, etc., but I want to know the
      > values of those parameters too.
      >
      > I'm trying to write a generic error handler. I wan't to
      > be able to log the values that caused the error, without
      > the user of the class having to specify the values 'cause
      > they're lazy ;)
      >
      > Thanks,
      > Brad[/color]

      MethodBase.GetC urrentMethod() returns the method that you're inside right
      now. From there you can move on to get all the params to it including
      values.
      --
      Roy Osherove
      weblog: http://www.iserializable.com

      Comment

      • Brad Quinn

        #4
        Re: Getting values of parameters to method

        StackFrame has a method named GetMethod() that returns a
        MethodBase.

        MethodBase has a method named GetParameters() that
        returns a ParameterInfo [].

        The problem is that ParameterInfo (understandably ) does
        not contain the values.

        However, there must be references to the parameters on
        the call stack. I should think that there might be a way
        to get at them, possibly through the StackFrame.

        I was hoping that ArgIterator might help, but I can't
        figure out how to intialize one (don't know how to get a
        RuntimeArgument Handle).

        Thanks anyway.
        [color=blue]
        >-----Original Message-----
        >On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:
        >
        >
        >MethodBase.Get CurrentMethod() returns the method that[/color]
        you're inside right[color=blue]
        >now. From there you can move on to get all the params to[/color]
        it including[color=blue]
        >values.
        >--
        >Roy Osherove
        >weblog: http://www.iserializable.com
        >.
        >[/color]

        Comment

        • Eric Johannsen

          #5
          Re: Getting values of parameters to method

          If you really want to do this, you received some good answers already.

          I feel that this is bad architectural practice because it obscures what is
          really happening in the error handlers, and it adds unnecessary complexity
          to the code base.

          The axim "Just because you can do something doesn't mean you should do it"
          applies farily often to reflection - keep it simple.

          Eric

          "Brad Quinn" <brad_quinn@yah oo.com> wrote in message
          news:013001c3a9 4a$e45f22e0$a40 1280a@phx.gbl.. .[color=blue]
          > Is there a way to get the values of the paramaters to a
          > method programatically ?
          >
          > I know that I can use reflection to find out the
          > parameter names and types, etc., but I want to know the
          > values of those parameters too.
          >
          > I'm trying to write a generic error handler. I wan't to
          > be able to log the values that caused the error, without
          > the user of the class having to specify the values 'cause
          > they're lazy ;)
          >
          > Thanks,
          > Brad[/color]

          Comment

          • Sebastien Lambla

            #6
            Re: Getting values of parameters to method

            I did a lot of research following this question, and there's no way to do it
            using .net.

            Only way to go is :
            - hijack in the debugging sdk and attach a debugger to the process to get
            the parameter values.
            - intercept calls using message sinks and contextboumd objects
            - know in advance your parameters.

            Arguments are not on the "call stack" but on the "stack" itself, so you
            can't really walk that stack yourself.

            --
            Sebastien Lambla



            "Brad Quinn" <anonymous@disc ussions.microso ft.com> a écrit dans le message
            de news: 00db01c3a950$de ae5270$a501280a @phx.gbl...[color=blue]
            > StackFrame has a method named GetMethod() that returns a
            > MethodBase.
            >
            > MethodBase has a method named GetParameters() that
            > returns a ParameterInfo [].
            >
            > The problem is that ParameterInfo (understandably ) does
            > not contain the values.
            >
            > However, there must be references to the parameters on
            > the call stack. I should think that there might be a way
            > to get at them, possibly through the StackFrame.
            >
            > I was hoping that ArgIterator might help, but I can't
            > figure out how to intialize one (don't know how to get a
            > RuntimeArgument Handle).
            >
            > Thanks anyway.
            >[color=green]
            > >-----Original Message-----
            > >On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:
            > >
            > >
            > >MethodBase.Get CurrentMethod() returns the method that[/color]
            > you're inside right[color=green]
            > >now. From there you can move on to get all the params to[/color]
            > it including[color=green]
            > >values.
            > >--
            > >Roy Osherove
            > >weblog: http://www.iserializable.com
            > >.
            > >[/color][/color]


            Comment

            • Brad Quinn

              #7
              Re: Getting values of parameters to method

              Thanks,

              That's what I thought.
              [color=blue]
              >-----Original Message-----
              >I did a lot of research following this question, and [/color]
              there's no way to do it[color=blue]
              >using .net.
              >
              >Only way to go is :
              >- hijack in the debugging sdk and attach a debugger to [/color]
              the process to get[color=blue]
              >the parameter values.
              >- intercept calls using message sinks and contextboumd [/color]
              objects[color=blue]
              >- know in advance your parameters.
              >
              >Arguments are not on the "call stack" but on the "stack" [/color]
              itself, so you[color=blue]
              >can't really walk that stack yourself.
              >
              >--
              >Sebastien Lambla
              >http://thetechnologist.is-a-geek.com/blog/
              >
              >
              >"Brad Quinn" <anonymous@disc ussions.microso ft.com> a [/color]
              écrit dans le message[color=blue]
              >de news: 00db01c3a950$de ae5270$a501280a @phx.gbl...[color=green]
              >> StackFrame has a method named GetMethod() that returns [/color][/color]
              a[color=blue][color=green]
              >> MethodBase.
              >>
              >> MethodBase has a method named GetParameters() that
              >> returns a ParameterInfo [].
              >>
              >> The problem is that ParameterInfo (understandably ) does
              >> not contain the values.
              >>
              >> However, there must be references to the parameters on
              >> the call stack. I should think that there might be a [/color][/color]
              way[color=blue][color=green]
              >> to get at them, possibly through the StackFrame.
              >>
              >> I was hoping that ArgIterator might help, but I can't
              >> figure out how to intialize one (don't know how to get [/color][/color]
              a[color=blue][color=green]
              >> RuntimeArgument Handle).
              >>
              >> Thanks anyway.
              >>[color=darkred]
              >> >-----Original Message-----
              >> >On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:
              >> >
              >> >
              >> >MethodBase.Get CurrentMethod() returns the method that[/color]
              >> you're inside right[color=darkred]
              >> >now. From there you can move on to get all the params [/color][/color][/color]
              to[color=blue][color=green]
              >> it including[color=darkred]
              >> >values.
              >> >--
              >> >Roy Osherove
              >> >weblog: http://www.iserializable.com
              >> >.
              >> >[/color][/color]
              >
              >
              >.
              >[/color]

              Comment

              • Brad Quinn

                #8
                Re: Getting values of parameters to method

                I'm trying to keep things as simple as possible.

                Obviously, for any particular method I know the
                parameters. Suppose I have a method like this;

                public int Compute( int a, int b )
                {
                try {
                return a * b;
                } catch ( Exception ex ) {
                Log.Write( ex, string.Format( "a={0}, b={1}", a,
                b ) );
                throw;
                }
                }

                When an exception is thrown I log the exception and the
                parameters to the method, ignoring the fact that (in
                general) member variables may have contributed to the
                exception.

                Now suppose that the method is changed, but that the lazy
                programmer forgets to update the logging statement

                public int Compute( int a, int b, int c )
                {
                try {
                return a * b * c;
                } catch ( Exception ex ) {
                Log.Write( ex, string.Format( "a={0}, b={1}", a,
                b ) );
                throw;
                }
                }

                If we look at the logs, we might be confused as to why
                the exception is being thrown. I'm trying to make error
                logging simpler (for the consumer).

                Thanks for your concern.
                [color=blue]
                >-----Original Message-----
                >If you really want to do this, you received some good[/color]
                answers already.[color=blue]
                >
                >I feel that this is bad architectural practice because[/color]
                it obscures what is[color=blue]
                >really happening in the error handlers, and it adds[/color]
                unnecessary complexity[color=blue]
                >to the code base.
                >
                >The axim "Just because you can do something doesn't mean[/color]
                you should do it"[color=blue]
                >applies farily often to reflection - keep it simple.
                >
                >Eric
                >
                >"Brad Quinn" <brad_quinn@yah oo.com> wrote in message
                >news:013001c3a 94a$e45f22e0$a4 01280a@phx.gbl. ..[color=green]
                >> Is there a way to get the values of the paramaters to a
                >> method programatically ?
                >>
                >> I know that I can use reflection to find out the
                >> parameter names and types, etc., but I want to know the
                >> values of those parameters too.
                >>
                >> I'm trying to write a generic error handler. I wan't[/color][/color]
                to[color=blue][color=green]
                >> be able to log the values that caused the error,[/color][/color]
                without[color=blue][color=green]
                >> the user of the class having to specify the[/color][/color]
                values 'cause[color=blue][color=green]
                >> they're lazy ;)
                >>
                >> Thanks,
                >> Brad[/color]
                >
                >.
                >[/color]

                Comment

                Working...