简体   繁体   中英

Get reference of ParameterInfo parameter

I have the method

     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();

How would I be able to get a reference to where firstParam parameter points to in order to get it's value?

I know I could get that by PersonName but I will like to retrive that info through firstParam.

The reflection information doesnt contain data on the current state of the object, just metadata on the parameter info. Even when you have a PropertyInfo class, you have to provide an object for it to have any meaning of the value. You're better off simply using the provided value of the parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM