0

在这种方法中,我写了一条错误消息。我需要写出使用此方法而不是 {0} 的当前类的名称。希望我的问题很清楚。谢谢。

class WriteEx
{
    public void WriteFormatExceptionError(string value, Exception ex, Logger logger)
    {
        string message = string.Format("Error occured in {0}. Cannot convert input string to double. Input string value: {1}.", **class name that used this method**, value);
        logger.WriteLog(message, ex);
        Console.WriteLine(ex.Message);
    }
}
4

1 回答 1

0

Be aware that System.Diagnostics.StackTrace is quite slow!

var st = new System.Diagnostics.StackTrace();

if (st.FrameCount > 1) {
    // GetFrame(0) returns the current method.
    var frame = st.GetFrame(1);
    var method = frame.GetMethod();
    var methodName = method.Name;

    // method.DeclaringType is a Type. You could even use
    // method.DeclaringType.FullName
    var methodClass = method.DeclaringType.Name;
}
于 2013-08-04T18:07:05.170 回答