简体   繁体   中英

Method Parameters C#

I wrote a method like this in C#.

 MethodBase method = MethodBase.GetCurrentMethod();
 string key ="";
 for (int i = 0; i < method.GetParameters().Length; i++)
 {
     key=method.GetParameters().Name;
     // need value of parameter here             
 } 

I'm getting parameter names through the above code. My question is: How can I get the values of the parameters which are coming to my method?

You can't - not without using the debugger API, at least (which is distinctly non-trivial). That information isn't available via reflection. In particular, the MethodBase object you're fetching is probably going to be the same one on every invocation.

(I don't think the method you've written is quite as you've shown either, and I really hope you're not calling GetParameters inside a loop like that, but that's a side issue.)

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