2013-08-04 3 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; 
} 
Смежные вопросы