문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top