2013-08-04 4 views
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); 
    } 
} 

답변

0

System.Diagnostics.StackTrace은 매우 느립니다.

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; 
}