10

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ash
  • 24,276
  • 34
  • 107
  • 152
  • Possible duplicate of [Getting value of parms using reflection](https://stackoverflow.com/questions/1867482/getting-value-of-parms-using-reflection) – Tot Zam Nov 01 '17 at 19:34

3 Answers3

15

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...

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    At my question http://stackoverflow.com/questions/2729580/how-to-get-the-parameter-names-of-an-objects-constructors-reflection people answer than you cannot get parameter names either -- maybe this is why you got downvoted. – Tom Apr 28 '10 at 13:09
  • 2
    With Reflection you can get parameter names: http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx – Nelson Rothermel Jun 15 '10 at 18:32
  • @JonSkeet This is basically a duplicate of your answer [here](https://stackoverflow.com/a/1867496/4660897), only that answer has some nice links and that question is formatted a little better. – Tot Zam Nov 01 '17 at 19:43
  • @TotZam: Well, and that answer was written nearly 11 months later... I'm not quite sure what the purpose of your comment is. – Jon Skeet Nov 01 '17 at 19:45
  • @JonSkeet I originally was thinking that it might make sense to update this answer with the additional information, or at least have a link pointing to the answer with more information. If the questions are really the same though, then it probably makes sense to just close one of the question as a duplicate. – Tot Zam Nov 01 '17 at 22:38
4

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.

Scott Muc
  • 2,873
  • 27
  • 38
-2

This is what I've come up with (however, it may not be particularly efficient):

MethodBase method = MethodBase.GetCurrentMethod();
string key = method.Name + "(";
for (int i = 0; i < method.GetParameters().Length; i++) {
  key += method.GetParameters().GetValue(i);
  if (i < method.GetParameters().Length - 1)
    key += ",";
}
key += ")";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ash
  • 24,276
  • 34
  • 107
  • 152
  • 3
    While the most important point is that I don't think this does what you think it does, yes it's inefficient - in various ways. Would you like to see a more efficient version which does the same thing, just for educational value? – Jon Skeet Jan 23 '09 at 07:38
  • 1
    (looped string concatenation vs StringBuilder, multiple calls to GetParameters(), etc). – Marc Gravell Jan 23 '09 at 08:04
  • 1
    @JonSkeet - I would really appreciate it if you demonstrated the proper implementation of this method. I would like to use it in conjunction with Diagnostic.Stopwatch to performance monitor some methods depending on parameters passed. Basically looking for "Method A (paramA = 1, paramB = 2) took 00:00:03.023 to execute. – Talon Aug 03 '15 at 13:04
  • 1
    @Talon: See my answer, and Scott Muc's. – Jon Skeet Aug 03 '15 at 13:05
  • Inefficient? Yes. Do what i want? Yes... Thanks! I will improve this but its great – ManuelCanepa Dec 12 '18 at 18:46