Alex Rivera | Logout

Is there any way to do something like this in C#?

Asked 2010-04-30T13:40:34.267
10

Is there any way to do something like this in C#?

public void DoSomething(string parameterA, int parameterB)
{

}

var parameters = ("someValue", 5);
DoSomething(parameters);
Edit
Report

2 Answers

10

No need to use reflection if you first store as a delegate, but it does require a strong declaration of the delegate.

public void DoSomething(string parameterA, int parameterB)
{
    Console.WriteLine(parameterA+" : "+parameterB);
}
void Main()
{

    var parameters = new object[]{"someValue", 5};
    Action<string,int> func=DoSomething;
    func.DynamicInvoke(parameters);

}

...and you can forget about compile-time type/sanity checking of the parameter list. Probably a bad thing.

answered 2010-04-30T13:47:51.567
0

"var" just represents a particular type, it's effectively shorthand for writing a type name. In the above you're not specifying any type. The only way to do this is to make a parameter class to represent the inputs in bulk...

public void DoSomething(Parameters param)
{
...
}

var param = new Parameters("someValue", 5);
DoSomething(param);

...but this is only going to be useful in specific circumstances. You could make multiple Parameters constructors to represent different arrangements of parameters, but the function you're calling will only take ONE input - the Parameters object. So with this you're really undermining the ability to overload a function.

So, in short, no. =)

answered 2010-04-30T13:48:24.223

Your Answer