Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes definitely.</p> <p>If you want an object to manipulate I actually use a function like this:</p> <pre><code>public static T CreateWrapper&lt;T&gt;(Exception innerException, params object[] parameterValues) where T : Exception, new() { if (parameterValues == null) { parameterValues = new object[0]; } Exception exception = null; StringBuilder builder = new StringBuilder(); MethodBase method = new StackFrame(2).GetMethod(); ParameterInfo[] parameters = method.GetParameters(); builder.AppendFormat(CultureInfo.InvariantCulture, ExceptionFormat, new object[] { method.DeclaringType.Name, method.Name }); if ((parameters.Length &gt; 0) || (parameterValues.Length &gt; 0)) { builder.Append(GetParameterList(parameters, parameterValues)); } exception = (Exception)Activator.CreateInstance(typeof(T), new object[] { builder.ToString(), innerException }); return (T)exception; } </code></pre> <p>This line:</p> <pre><code>MethodBase method = new StackFrame(2).GetMethod(); </code></pre> <p>Walks up the stack frame to find the calling method then we use reflection to obtain parameter information values passed to it for a generic error reporting function. To get the current method simply use current stack frame (1) instead.</p> <p>As others have said for the current methods name you can also use:</p> <pre><code>MethodBase.GetCurrentMethod() </code></pre> <p>I prefer walking the stack because if look internally at that method it simply creates a StackCrawlMark anyway. Addressing the Stack directly seems clearer to me </p> <p>Post 4.5 you can now use the [CallerMemberNameAttribute] as part of the method parameters to get a string of the method name - this may help in some scenarios (but really in say the example above)</p> <pre><code>public void Foo ([CallerMemberName] string methodName = null) </code></pre> <p>This seemed to be mainly a solution for INotifyPropertyChanged support where previously you had strings littered all through your event code.</p>
 

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