我怎样才能得到一个调用方法的参数的值? [英] How can I get the values of the parameters of a calling method?

查看:218
本文介绍了我怎样才能得到一个调用方法的参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我在写一些code,需要能够从调用到类中的方法得到的参数在。我知道如何让所有的方式到的ParameterInfo []数组,但我不知道如何再拿到价值。这甚至可能?

如果是这样,我认为这是与使用MethodBody酒店需要从MethodInfo的对象,它可以让你检查IL流,包括属性,但我不知道该怎么做,我的天堂T发现在谷歌应用code。

code

  //调用查找从类方法调用到这一个
公共类SomeClass的
{
    公共静态无效FindMethod()
    {
        的for(int i = 1; I< frameCount;我++)
        {
            VAR帧=新的StackFrame(I)
            VAR MethodInfo的= frame.GetMethod();
            如果(methodInfo.DeclaringType!= this.GetType())
            {
                字符串的方法名= frame.GetMethod()名称。
                变种paramInfos = methodInfo.GetParameters();                // 怎么办??我如何从paramInfos值                打破;
            }
            否则如果(ⅰ== frameCount - 1)
            {
                抛出新TransportException(找不到方法名);
            }
        }
    }
}


解决方案

您不能做到这一点,而不反思栈自己(这是脆弱的,因为许多的优化可能意味着堆栈帧不是你所期望的,甚至可以说参数通过其实也不是方法签名会建议什么(这是完全有可能的优化JIT编译器被发现,你只能使用一个对象/结构的子领域,并传递代替)。

信息参数简单地告诉你的签名法的已编译的,而不是被传递的值。

来自动实现这一目标的唯一现实的途径是通过code注射液(通过像AOP)来创建数据,做你想做的事情的基础上分析了IL什么。

这通常不是一个好主意,如果你需要一些调试使用调试器,如果您需要登录的东西是明确你正在登录的东西。

需要明确的是简单的反射技术的不能实现你以饱满的通用渴望

Question

I'm writing some code that needs to be able to get the values of the parameters from the method that called into the class. I know how to get all the way to the ParameterInfo[] array, but I don't know how to then get the values. Is this even possible?

If it is, I think it has something to do with using the MethodBody property from the MethodInfo object, which allows you to inspect the IL stream, including properties, but I don't know how to do it, and I haven't found applicable code on Google.

Code

// Finds calling method from class that called into this one
public class SomeClass
{
    public static void FindMethod()
    {
        for (int i = 1; i < frameCount; i++)
        {
            var frame = new StackFrame(i);
            var methodInfo = frame.GetMethod();
            if (methodInfo.DeclaringType != this.GetType())
            {
                string methodName = frame.GetMethod().Name;
                var paramInfos = methodInfo.GetParameters();

                // Now what?? How do I get the values from the paramInfos

                break;
            }
            else if (i == frameCount - 1)
            {
                throw new TransportException("Couldn't find method name");
            }
        }
    }
}

解决方案

You cannot do it without introspecting the stack yourself (and this is fragile since many optimizations may mean the stack frame is not what you expect, or even that the parameter passed is not in fact what the method signature would suggest (it is perfectly possible for an optimizing JIT compiler to spot that you are only using a sub field of an object/struct and pass that instead).

The ParameterInfo simply tells you the signature of the method as compiled, not the values that were passed.

The only realistic way to achieve this automatically is via code injection (via something like AOP) to create the data and do what you want with it based on analysing the IL.

This is generally not a good idea, if you need to debug something use a debugger, if you need to log something be explicit about what you are logging.

To be clear simple reflective techniques cannot achieve what you desire with full generality

这篇关于我怎样才能得到一个调用方法的参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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