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

W

waylonflinn

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?
 
C

Carl Daniel [VC++ MVP]

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.GetMethod(),
System.Reflection.MethodInfo.Invoke().

-cd
 
C

Chris Nahr

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top