1

我有方法

     public void SomeMethod(string PersonName,int Age)
     {

        // get call stack
        StackTrace stackTrace = new StackTrace();

        // should equal "SomeMethod"
        var MethodName = stackTrace.GetFrame(0).GetMethod().Name;

        // should be ParameterInfo of parameter "PersonName"
        var firstParam = stackTrace.GetFrame(0).GetMethod().GetParameters()[0];

        // Here is where I get stuck !!!!!!!
        var t = firstParam.GetValue();

我如何能够获得对 firstParam 参数指向的位置的引用以获取它的值?

我知道我可以通过 PersonName 获得该信息,但我想通过 firstParam 检索该信息。

4

1 回答 1

3

反射信息不包含对象当前状态的数据,只是参数信息的元数据。即使你有一个PropertyInfo类,你也必须为它提供一个对象以使其具有任何值的含义。最好只使用提供的参数值。

于 2012-05-18T18:41:43.313 回答