如何在C#中获取调用方法的属性

3
我正在为我的应用程序编写错误记录器。基本上,我感兴趣的是在处理异常时,从我的日志类中调用一个名为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;
    }

非常感谢您的帮助。
1个回答

1
MorpheeException 构造函数中,你可以使用反射 查找调用方法,然后使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接