问题
我正在编写一些代码,这些代码需要能够从调用类的方法中获取参数的值。我知道如何一直到 ParameterInfo[] 数组,但我不知道如何获取这些值。这甚至可能吗?
如果是,我认为它与使用 MethodInfo 对象中的 MethodBody 属性有关,它允许您检查 IL 流,包括属性,但我不知道该怎么做,我还没有找到Google 上的适用代码。
代码
// 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");
}
}
}
}