Question

I want to print the current method call (incl. return value) to the Visual Studio Output like this:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    Func<object, object> ret = (value) =>
    {
#if DEBUG            
        var debug = new StringBuilder();
        debug.Append("MyConverter.Convert([");
        debug.Append(values.Sum(v => (v != null ? v.ToString() : "null") + ',', null, v => v != null ? v.ToString() : "null"));
        debug.Append("], " + targetType.ToString() + ", " + parameter.ToString() + ", " + culture.DisplayName + ") =" + value.ToString() + ";");
        Debug.WriteLine(debug.ToString());
#endif
        return value;
    };

 // [..]
}

I'm using this sometimes to achieve more informations (e.g. from a Converter as shown here) while debugging. However, that's just a roundabout way.

Is there any way to do it more flexible? Something like GetCurrentArguments (from MethodInfo)?

Was it helpful?

Solution

Since you are using it for debugging there is an option using the StackTrace and StackFrame To get the current method name, but you wont get the arguments, and there is a severe performance penalty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top