Get parameters and their values from IMethodInvocation

Jun 5, 2012 at 8:45 AM
Edited Jun 5, 2012 at 8:48 AM

Hi,

I'd like to know how to get the parameters and their values from IMethodInvocation input.

Considering a method which has basic types as parameters (string, int, etc) that would be easily achieved by the following:

 

input.Arguments.GetParameterInfo(i).Nam;

input.Arguments[i];

 

However when the method has a mixture of parameters (e.g. string, objects), it becomes difficult to retrieve the parameters and the values inside the object-parameter.

My code snippet below:

 

            var str = "";
            for (int i = 0; i < input.Arguments.Count; ++i )
            {
                var paramType = input.Arguments.GetParameterInfo(i).ParameterType.Name.ToLower();

                if(!paramType.StartsWith("obj")) // in that case object definition need to start with "Obj*"
                {
                    str += input.Arguments.GetParameterInfo(i).Name + " - " + input.Arguments[i] + " | ";
                }
                else
                {
                    foreach (var value in input.Arguments) // i ??
                    {
                        PropertyInfo[] properties = value.GetType().GetProperties();

                        str += properties.Aggregate(str, (current, pi) => current + (pi.Name + " - " + pi.GetValue(value, null) + " | "));
                    }
                }
            }


The "foreach" look does not work properly since it does not use the "i" to get the current parameter but instead fetches the params from the start.

Method looks like: GetUser(string param1, string param1, object param3)

My expected result would be to build a string like:

"param1 - value | param2 - value | param3.paramA - value | param3.paramB - value ..."

Any advices are most welcome.

Thanks,

Yohan
Jun 7, 2012 at 10:12 AM

> I'd like to know how to get the parameters and their values from IMethodInvocation input.

Can you expand on what you are trying to do?  i.e. what problem are you trying to solve?  It looks like you want to dump the parameters to a log?

If so, then take a look at this: http://stackoverflow.com/questions/360277/what-is-the-best-way-to-dump-entire-objects-to-a-log-in-c . It has some good suggestions such as basing off of the ObjectDumper LINQ sample, or using serialization.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jun 7, 2012 at 11:08 AM

Hi,

Actually I am intercepting specific methods to implement a custom caching mechanism.  In order to build a good cache key, I wanted to get the namespace, method's name, parameters and their respective values so as to build the cache key.

I've been able to achieve the above using the unity's IMethodInvocation (input.inputs, Arguments, ArgumentInfo, ... etc)

 

Thanks,

Yohan