Capturing input argument values ​​in a KeyValuePair array

I am wondering how I can get the input arguments of my method in an array. Or just retrieve the values ​​of my arguments dynamically.

Value, type call:

MyMethod(10, "eleven"); 

For the method:

 void MyMethod(int Test, str Test2) {} 

Will be allowed in an array like:

 {{"Test" => 10}, {"Test2", "eleven"}} 

It would be even better if I could achieve this with reflection. somehow with StackTrace.

+6
reflection c # arguments
source share
6 answers

I think that what you are looking for does not exist. The closest you have are the options:

 MyMethod(params object[] args) { // if you have to do this, it 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 compilation time type check, and therefore this is not a universal way of handling arguments. But if your arguments are dynamic, this is probably the solution.


Edit: there was another idea:

You need to put all arguments manually in 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); } 
+3
source share

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

+2
source share

Good question (+1). I think this is what you need -

 MethodBase mb = MethodBase.GetCurrentMethod(); ParameterInfo[] pi = mb.GetParameters(); 
0
source share

One of the ways that I know (not sure if this is the only way these days, but it was before) is to use aspect-oriented programming (AOP) and, in particular, interception. It is a little painful to throw it by hand, but there are great tools that come to the rescue. One such tool is PostSharp: http://www.postsharp.org/ .

0
source share

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

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

I'm not sure if you ask how you explained this, it makes sense. Perhaps you could stop in more detail?

0
source share

It may not be exactly what you were looking for, but I found that I can get a reasonable compromise for my situation, using variations of the Matt Hamilton method proposed and using an implicit designation of parameters of an anonymous type:

 public void MyMethod(string arg1, bool arg2, int arg3, int arg4) { var dictionary = new PropertyDictionary(new { arg1, arg2, arg3, arg4 }); } public class PropertyDictionary : Dictionary<string, object> { public PropertyDictionary(object values) { if(values == null) return; foreach(PropertyDescriptor property in TypeDescriptor.GetProperties(values)) Add(property.Name, property.GetValue(values); } } 

As I said, this may not be useful in your situation, but in my (unit testing a method that processes XML) it was very useful.

0
source share

All Articles