使用反射分析参数及其值 [英] Using Reflection to analyze Parameters and their values

查看:72
本文介绍了使用反射分析参数及其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到过一些关于SO的较早的帖子,大约只有一年的历史,这意味着它们实际上并没有涵盖.NET 4甚至3.5主题。这样就可以了。

I've seen older posts here on SO, about one year old which would mean that they do not really cover .NET 4 or maybe even 3.5 on this topic. So here goes.

如果通过反射将获取当前方法的参数

If you with reflection were to fetch parameters for the current method

ParameterInfo[] methodParams = MethodInfo.GetCurrentMethod().GetParameters();

遍历每个参数将允许您获取参数名称,但是只有一个 DefaultValue我猜是因为.NET 4中新的动态参数。

Looping through each parameter will let you fetch the parameter-name however, there is only a "DefaultValue" which I guess is there because of the new Dynamic Parameters in .NET 4.

但是,我的问题是;

我知道如果您甚至需要考虑使用此功能,可能会存在设计缺陷。

I know that there might be a design flaw if you even need to consider using this.

推荐答案

不使用性能分析API便无法获取当前参数值。

It is not possible to get the current parameter values without using the Profiling API.

MethodInfo 对象是按方法而不是按调用的。无法将 MethodInfo 与给定的堆栈框架连接。

MethodInfo objects are per-method, not per-call. There is no way to connect a MethodInfo with a given stack frame.

此外,在Release版本中,可以对参数局部变量进行优化,因此值不一定存在

In addition, in Release builds, the parameter locals can be optimized out, so the values to not necessarily exist.

DefaultValue 属性在VB参数中可以为非 null ,该参数已经支持默认值。

The DefaultValue property can be non-null in VB parameters, which already supports default values.

这篇关于使用反射分析参数及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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