Is there a way in .NET to know what parameters and their values were passed to a method. Reflection way? This will be used from inside the method. It has to be generic so it can be used from any method. This is for logging purposes.
-
I ended up making this [User Activity Logging, Telemetry and Variables in Global Exception Handlers project](https://stackoverflow.com/questions/30326673/user-activity-logging-telemetry-and-variables-in-global-exception-handlers) – Jeremy Thompson Jul 10 '17 at 01:27
7 Answers
Call MethodBase.GetCurrentMethod().GetParameters()
.
However, it is not possible to get the parameter values; due to JIT optimization, they might not even exist anymore.

- 868,454
- 176
- 1,908
- 1,964
MethodInfo.GetCurrentMethod()
will give you information about the current method and then get information about the parameters using GetParameters()
.

- 1,023,142
- 271
- 3,287
- 2,928
What you're trying to do can be achieved easily using aspect oriented programming. There are good tutorials online, I'll point to two of them:

- 2,229
- 14
- 15
-
+1 AFAIK this is the only efficient way to achieve this goal. The only other way would be to use another process to debug the current process, nasty, unreliable, and slow. – csharptest.net Jan 17 '11 at 20:09
-
1Why the downvote? AOP/weaving could be one solution for the problem. – Christian Klauser Jan 17 '11 at 20:11
-
aspect oriented programming is a methodology. It still translates eventually to some .NET api's. – Tony_Henrich Jan 17 '11 at 20:28
public void FunctionWithParameters(string message, string operationName = null, string subscriptionId = null)
{
var parameters = System.Reflection.MethodBase.GetCurrentMethod().GetParameters();
this.PrintParams(parameters, message, operationName, subscriptionId);
}
public void PrintParams(ParameterInfo[] paramNames, params object[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine($"{paramNames[i].Name} : {args[i]}");
}
}

- 183
- 1
- 12
-
not bad but you need to remember to pass in all the parameter's values or the log will end up being innacurate. – Andrew May 30 '22 at 18:15
You need AOP to achieve what you are looking for. in c# you can use DispatchProxy to do that. Check the following How to wrap existing object instance into DispatchProxy?

- 1,596
- 13
- 13
Nowadays a feature that could be used to achieve this is Roslyn's Source Generators.
In this way, the code that gets the parameters' values would be generated at compile-time based on the method definition. Could be interpreted as “compile-time reflection”.
Let's show an example to try to explain it better:
public void MethodInspectingItsOwnParameters(
[Description("First parameter")]
string paramName_1,
[Description("Second parameter")]
int paramName_2,
// ...
[Description("N-th parameter")]
object paramName_N,
// ...
[Description("Last parameter")]
bool paramName_M)
{
var paramsAndValues = new List<KeyValuePair<string, object>>();
// -
// => Put here the code that, using Roslyn's compile time
// metaprogramming, inspect the parameters from the method
// definition and at compile time generate the code that
// at run time will get the parameters's values and load
// them into [paramsAndValues]
// -
// #Rosalyn generated code
// · Code autogenerated at compile time
// => loads parameter's values into [paramsAndValues]
// -
// Eg (Hypothetical example of the code generated at compile
// time by Roslyn's metaprogramming):
//
// paramsAndValues.Add("paramName_0", paramName_1);
// ...
// paramsAndValues.Add("paramName_N", paramName_N);
// ...
// paramsAndValues.Add("paramName_M", paramName_M);
//
// - Note: this code will be regenerated with each compilation,
// so there no make sense to do nameof(paramName_N)
// to obtaint parameter's name
// #End Rosalyn generated code
foreach (var param in paramsAndValues)
{
string paramName = param.Key;
object paramValue = param.Value;
// In this section of the normal code (not generated at
// compile time) do what you require with the
// parameters/values already loaded into [paramsAndValues]
// by the compile time generated code
}
}

- 359
- 1
- 12
-
At this point, I have never *played* with Roslyn's Source Generators. This question would be a nice learning exercise to do. If I take the time, I'll come back here to add some (hopefully) working code to this answer. – CicheR Aug 06 '20 at 05:08
I had the same issue and here goes my solution for logging all the parameters and their values. ONLY wihtin a Controller Context:
public static string CallParams = "";
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
CallParams = JsonConvert.SerializeObject(filterContext.ActionParameters);
}
Hope it helps

- 21
- 3