Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reflection to get method name and parameters

I am trying to workout a way to programatically create a key for Memcached, based on the method name and parameters. So if I have a method,

string GetName(int param1, int param2);

it would return:

string key = "GetName(1,2)";

I know you can get the MethodBase using reflection, but how do I get the parameters values in the string, not the parameter types?

like image 603
Ash Avatar asked Jan 23 '09 02:01

Ash


2 Answers

You can't get method parameter values from reflection. You'd have to use the debugging/profiling API. You can get the parameter names and types, but not the parameters themselves. Sorry...

like image 138
Jon Skeet Avatar answered Nov 02 '22 20:11

Jon Skeet


What you're looking for is an interceptor. Like the name says, an interceptor intercepts a method invocation and allows you to perform things before and after a method is called. This is quite popular in many caching and logging frameworks.

like image 42
Scott Muc Avatar answered Nov 02 '22 21:11

Scott Muc