Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I came up with as a solution:</p> <p>PostSharp or another AOP solution wasn't really practical in this situation, so unfortunately I had to abandon that idea.</p> <p>It appears that while it is possible to parameter names and types using reflection, the only way to access the runtime values is with a debugger attached.</p> <p>See here for more info:</p> <p><a href="https://stackoverflow.com/questions/157911/in-a-net-exception-how-to-get-a-stacktrace-with-argument-values">StackOverflow</a></p> <p><a href="http://groups.google.co.uk/group/microsoft.public.dotnet.framework/browse_thread/thread/1bf8888efb2c7076/de051fc487c3900?hl=en&amp;" rel="nofollow noreferrer">microsoft.public.dotnet.framework</a></p> <p>So that still left me with the problem of ~50 methods that needed this logging adding by hand.</p> <p>Reflection to the rescue...</p> <pre><code>public String GetMethodParameterArray() { var output = new StringBuilder(); output.AppendLine(); Type t = typeof(API); foreach (var mi in t.GetMethods()) { var argsLine = new StringBuilder(); bool isFirst = true; argsLine.Append("object[] args = {"); var args = mi.GetParameters(); foreach (var pi in args) { if (isFirst) { isFirst = false; } else { argsLine.Append(", "); } argsLine.AppendFormat("{0}", pi.Name); } argsLine.AppendLine("};"); //close object[] initialiser output.AppendLine(argsLine.ToString()); output.AppendFormat("Log(\"{0}\",args);", mi.Name); output.AppendLine(); output.AppendLine(); } return output.ToString(); } </code></pre> <p>This code snippet loops through the methods on a class and outputs an object[] array initialised with the arguments passed into the method and a Log call containing the arguments and the method name.</p> <p>Example output:</p> <pre><code>object[] args = {username, password, name, startDate, endDate, cost}; Log("GetAwesomeData",args); </code></pre> <p>This block can then be pasted into the top of the method to achieve the required effect.</p> <p>It is more manual than I would have liked, but it is a lot better than having to type the parameters by hand and far less error prone.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload