Pergunta

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)?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top