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

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

问题描述

说我有一个方法:

 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的方法签名,因为它是暴露一个We​​bMethod,具有复制遗留系统被仿冒。

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:

<一个href=\"http://stackoverflow.com/questions/157911/in-a-net-exception-how-to-get-a-stacktrace-with-argument-values\">StackOverflow

<一个href=\"http://groups.google.co.uk/group/microsoft.public.dotnet.framework/browse_thread/thread/1bf8888efb2c7076/de051fc487c3900?hl=en&\"相对=nofollow> 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();
    }

这code片段遍历一个类的方法和输出对象[]数组传入方法和包含的参数和方法名登录调用的参数进行初始化。

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天全站免登陆