Invoke Methods with Arbitrary (Known) Parameters From a Single Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • waylonflinn@gmail.com

    Invoke Methods with Arbitrary (Known) Parameters From a Single Method

    I'm looking for a way to invoke methods with an arbitrary number of
    parameters of arbitrary type from within a single method, when those
    parameters are known at the time of invocation of the containing
    method. I don't see a reason why this shouldn't work. I also can't
    find the language feature which implements it. More details below.

    As part of the creation of a set of classes for doing unit testing I
    want to create a generic 'RunTestMethod' method. This method will
    encapsulate my reporting and data collection (whether a given test
    passed or failed, how many tests have passed or failed so far). In
    order to be useful it also needs to be capable of calling a method
    with an arbitrary number of parameters (whose values are known when
    the 'RunTestMethod' method is called). This is to preserve the
    generality of the testing framework.

    My first thought was to use delegates. This allows me to pass a
    variety of methods to another method and invoke them therein. However,
    I have yet to find a way of allowing a delegate to have arguments
    whose number and type differ from the method to be invoked, even when
    the arguments are known at the point of instantiation.

    Can any of you think of a way of implementing this that doesn't
    sacrifice generality?

  • Carl Daniel [VC++ MVP]

    #2
    Re: Invoke Methods with Arbitrary (Known) Parameters From a Single Method

    <waylonflinn@gm ail.comwrote in message
    news:1173734007 .811963.309830@ 8g2000cwh.googl egroups.com...
    I'm looking for a way to invoke methods with an arbitrary number of
    parameters of arbitrary type from within a single method, when those
    parameters are known at the time of invocation of the containing
    method. I don't see a reason why this shouldn't work. I also can't
    find the language feature which implements it. More details below.
    >
    As part of the creation of a set of classes for doing unit testing I
    want to create a generic 'RunTestMethod' method. This method will
    encapsulate my reporting and data collection (whether a given test
    passed or failed, how many tests have passed or failed so far). In
    order to be useful it also needs to be capable of calling a method
    with an arbitrary number of parameters (whose values are known when
    the 'RunTestMethod' method is called). This is to preserve the
    generality of the testing framework.
    >
    My first thought was to use delegates. This allows me to pass a
    variety of methods to another method and invoke them therein. However,
    I have yet to find a way of allowing a delegate to have arguments
    whose number and type differ from the method to be invoked, even when
    the arguments are known at the point of instantiation.
    >
    Can any of you think of a way of implementing this that doesn't
    sacrifice generality?
    You can do this using reflection. See System.Type.Get Method(),
    System.Reflecti on.MethodInfo.I nvoke().

    -cd


    Comment

    • Chris Nahr

      #3
      Re: Invoke Methods with Arbitrary (Known) Parameters From a Single Method

      On 12 Mar 2007 14:13:27 -0700, waylonflinn@gma il.com wrote:
      >Can any of you think of a way of implementing this that doesn't
      >sacrifice generality?
      The most general way would be to declare your delegate and its methods
      as receiving a params object[] array. That way they can all get an
      arbitrary number of arbitrary parameters. However, they have to
      figure out the type and purpose of these parameters on their own.
      --
      Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

      Comment

      Working...