Is there a way to get an array of the arguments passed to a method?

后端 未结 9 1344
小蘑菇
小蘑菇 2020-12-05 18:17

Say I have a method:

 public void SomeMethod(String p1, String p2, int p3)
 {

 #if DEBUG
    object[] args = GetArguments();
    LogParamaters(args);
 #endi         


        
9条回答
  •  被撕碎了的回忆
    2020-12-05 18:26

    Here's what I came up with as a solution:

    PostSharp or another AOP solution wasn't really practical in this situation, so unfortunately I had to abandon that idea.

    It appears that while it is possible to parameter names and types using reflection, the only way to access the runtime values is with a debugger attached.

    See here for more info:

    StackOverflow

    microsoft.public.dotnet.framework

    So that still left me with the problem of ~50 methods that needed this logging adding by hand.

    Reflection to the rescue...

    public String GetMethodParameterArray()
        {
            var output = new StringBuilder();
            output.AppendLine();
    
            Type t = typeof(API);
            foreach (var mi in t.GetMethods())
            {
                    var argsLine = new StringBuilder();
                    bool isFirst = true;
                    argsLine.Append("object[] args = {");
                    var args = mi.GetParameters();
    
                    foreach (var pi in args)
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                        }
                        else
                        {
                            argsLine.Append(", ");
                        }
                        argsLine.AppendFormat("{0}", pi.Name);
                    }
                    argsLine.AppendLine("};"); //close object[] initialiser
    
                    output.AppendLine(argsLine.ToString());
                    output.AppendFormat("Log(\"{0}\",args);", mi.Name);
                    output.AppendLine();
                    output.AppendLine();
                }
            return output.ToString();
        }
    

    This code snippet loops through the methods on a class and outputs an object[] array initialised with the arguments passed into the method and a Log call containing the arguments and the method name.

    Example output:

    object[] args = {username, password, name, startDate, endDate, cost};
    Log("GetAwesomeData",args);
    

    This block can then be pasted into the top of the method to achieve the required effect.

    It is more manual than I would have liked, but it is a lot better than having to type the parameters by hand and far less error prone.

提交回复
热议问题