有没有办法获取传递给方法的参数数组? [英] Is there a way to get an array of the arguments passed to a method?

查看:24
本文介绍了有没有办法获取传递给方法的参数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个方法:

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

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

     // Do Normal stuff in the method
 }

有没有办法检索传递给方法的参数数组,以便记录它们?

Is there a way to retrieve an array of the arguments passed into the method, so that they can be logged?

我有大量方法,希望避免手动将参数按名称传递给记录器,因为人为错误将不可避免地蔓延.

I have a large number of methods and want to avoid manually passing the arguments by name to the logger, as human error will inevitably creep in.

我猜它会涉及某种形式的反射 - 这很好,因为它只会用于调试目的.

I'm guessing it will involve reflection in some form - which is fine, as it will only be used for debugging purposes.

更新

更多信息:

我无法更改 SomeMethod 的方法签名,因为它作为 WebMethod 公开并且必须复制它所模拟的遗留系统.

I can't change the method signature of SomeMethod, as it is exposed as a WebMethod and has to replicate the legacy system it is impersonating.

遗留系统已经记录了传入的参数.从新实现开始,将包装遗留系统,所以我希望记录进入 C# 版本的参数,以便我可以验证正确的参数以正确的顺序传入.

The legacy system already logs the arguments that are passed in. To start with the new implementation will wrap the legacy system, so I'm looking to log the parameters coming into the C# version, so that I can verify the right parameters are passed in in the right order.

我只是想记录参数值和顺序,而不是它们的名称.

I'm just looking to log the argument values and order, not their names.

推荐答案

这是我想出的解决方案:

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

PostSharp 或其他 AOP 解决方案在这种情况下并不实用,所以很遗憾我不得不放弃这个想法.

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

所以这仍然给我留下了大约 50 种需要手动添加日志记录的方法的问题.

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

反思拯救...

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();
    }

此代码片段循环遍历类上的方法并输出一个 object[] 数组,该数组初始化为传递给方法的参数以及包含参数和方法名称的 Log 调用.

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.

示例输出:

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.

这篇关于有没有办法获取传递给方法的参数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆