我目前正在为我的应用程序写一个错误记录器,基本上我感兴趣的是处理异常时,我调用一个函数CreateLogEntry(...) from我的日志记录类。这个想法是,我想CreateLogEntry(...)知道它是从一个方法内调用BadFunction(...)并检索其参数作为日志记录过程的一部分。现在我已经做了一些家庭作业(我猜测虽然不够:S),并且我确信我的解决方案在于System.Reflection,并且已经找到了一些关于如何枚举类中的所有函数的示例( http://www.csharp-examples.net/get-method-names/)。 但是有可能获得创建该异常的函数的名称。如何获取调用方法属性在C#
EX(我得到了什么):
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#中的反射:我如何获得调用方法名称和类型?](http://stackoverflow.com/questions/3095696/reflection-in-c-how-do-i-get-该呼-方法的名称和类型) – Oliver 2011-03-30 07:59:53