【发布时间】:2011-07-25 21:07:45
【问题描述】:
我目前正在为我的应用程序编写一个错误记录器,基本上我感兴趣的是在处理异常时,我从我的日志记录类中调用一个函数 CreateLogEntry(...)。 这个想法是,我希望 CreateLogEntry(...) 知道它是从方法 BadFunction(...) 中调用的,并检索其参数作为日志记录的一部分过程。
现在我已经做了一些功课(虽然:S 我想还不够),我确信我的解决方案在于 System.Reflection,并且已经找到了一些关于如何枚举所有函数的示例在一个类中 (http://www.csharp-examples.net/get-method-names/)。 但是是否有可能获得创建该异常的函数的名称。
前(我得到的):
public string GetCurrentMode()
{
string currentMode = "";
try
{
currentMode = _host.Mode.ToString();
}
catch(Exception ex)
{
Common.MorpheeException me = new Common.MorpheeException();
me.MethodName = "GetCurrentMode";
me.NameSpace = "MorpheeScript";
me.ErrorNumber = 0;
me.ErrorDescription = ex.Message;
me.StackTrace = ex.StackTrace;
throw new FaultException<Common.MorpheeException>(me);
}
return currentMode;
}
我想拥有什么:
public string GetCurrentMode()
{
string currentMode = "";
try
{
currentMode = _host.Mode.ToString();
}
catch(Exception ex)
{
Common.MorpheeException me = new Common.MorpheeException(ex);
// Here me should know that it is called from GetCurrentMode() and
// retrieve the parameters if there are any
throw new FaultException<Common.MorpheeException>(me);
}
return currentMode;
}
非常感谢您的帮助
【问题讨论】:
标签: c# reflection exception-handling