使用反射PARMS的获取价值 [英] Getting value of parms using reflection
问题描述
我怎样才能得到PARMS的值(使用反射循环)。
在previous问题,有人向我展示了如何通过使用反射的PARMS循环。
How can I get the values of parms (in a loop using reflection). In previous question, someone showed me how to loop through the parms using reflection.
static void Main(string[] args)
{
ManyParms("a","b","c",10,20,true,"end");
Console.ReadLine();
}
static void ManyParms(string a, string b, string c, int d, short e, bool f, string g)
{
var parameters = MethodBase.GetCurrentMethod().GetParameters();
foreach (ParameterInfo parameter in parameters)
{
string parmName = parameter.Name;
Console.WriteLine(parmName);
//Following idea required an object first
//Type t = this.GetType();
//t.GetField(parmName).GetValue(theObject));
}
}
如果你一定要知道我为什么要做到这一点,请看这里:
.NET的所有方法参数的反射
If you must know why I want to do this, please see here: .NET Reflection of all method parameters
感谢所有 - 这似乎是在Python,Perl,PHP,这将是很容易结果。
虽然它可能不反映,如果我使用反射来获取字段名,好像会有一个简单的动态的方式来获得基于名称的值。
我没试过AOP(面向方面编程),但解决方案。
这是那些东西,如果我不能在一两个小时做,我可能不会做这件事。
Thanks all - it seems like in Python, PERL, PHP this would be easy.
Even though it may not be reflection, if I use reflection to get the field name, seems like there would be an easy dynamic way to get the value based on the name.
I haven't tried AOP (Aspect Oriented Programming) the solutions yet.
This is one of those things that if I can't do it in an hour or two, I probably won't do it.
推荐答案
您不能,基本上是 - 至少在没有钩住的调试器/剖析API 。
You can't, basically - at least not without hooking into the debugger/profiling API.
在理论上存在的可能的是的<一个某种方式href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe%5Fmembers.aspx\"><$c$c>StackFrame$c$c>类暴露参数值,但它不会 - 我怀疑,为了做到这一点,你必须删除一些的优化
In theory there could be some way of the StackFrame
class exposing parameter values, but it doesn't - and I suspect that in order to do so, you'd have to remove several optimisations.
这篇关于使用反射PARMS的获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!