views:

743

answers:

5

Hi,

I am wondering how I can fetch the incoming arguments of my method in an array. Or just retrieve the values of my arguments dynamicly.

Meaning, a call like: MyMethod(10, "eleven");

For method: void MyMethod(int Test, str Test2) {}

Would resolve in an array like: {{"Test" => 10}, {"Test2", "eleven"}}

Would be even better if I could achive this with reflection.. Ex. somehow with StackTrace.

+1  A: 

Your best option is to do it with an anonymous type, as shown in this example.

Matt Hamilton
+3  A: 

I think, what you are looking for does not exist. The closest you can have is params:

MyMethod(params object[] args) 
{
    // if you have to do this, it's quite bad:
    int intArg = (int)args[0];
    string stringArg = (string)arg[1]:
}

// call with any number (and type) of argument
MyMethod(7, "tr");

There is no compile time type checking, and therefore it is not an all-purpose way to handle arguments. But if your arguments are dynamic, it's probably a solution.


Edit: had another idea:

You need to put all argument manually into a list / dictionary. You can write a helper class to allow the following:

MyMethod(int arg1, string arg2) 
{
    Arguments.Add(() => arg1);
    Arguments.Add(() => arg2);
    //
}

The helper looks like this

public static void Add<T>(Expression<Func<T>> expr)
{
    // run the expression to get the argument value
    object value = expr.Compile()();
    // get the argument name from the expression
    string argumentName = ((MemberExpression)expr.Body).Member.Name;

    // add it to some list:
    argumentsDic.Add(argumentName, value);
}
Stefan Steinegger
The thing is that I'am using WebServices. I want to capture the incoming arguments of the call being made...
I had another idea... see the new section in my answer.
Stefan Steinegger
A: 

Nice Question (+1). I think this is what you need -

MethodBase mb = MethodBase.GetCurrentMethod();
ParameterInfo[] pi = mb.GetParameters();
Kirtan
But you won't get the argument values, will you?
Stefan Steinegger
I've actually already tried that. But I only get the names of the Arguments, not the values of the incoming/current call.
Stefan: Exactly..
A: 

One way I know (not sure if it's the only way nowadays, but it used to be) is to use aspect-oriented programming (AOP), and in particular interception. It is a bit painful to roll it by hand, but there are excellent tools that come to the rescue. One such tool is PostSharp: http://www.postsharp.org/.

Guido Domenici
A: 

Since the method is using named parameters, why can't you just explicitly populate a dictionary with their names and values? There is little point in using reflection to get their names since you already know them.

As already stated, the params keyword can be used to define a method with a variable number of parameters, but by definition those are nameless.

I'm not sure what you are asking, in the way you've explained it, makes any sense. Perhaps you could elaborate further?

Winston Smith