【问题标题】:How to get the calling method properties in c# [duplicate]如何在c#中获取调用方法属性[重复]
【发布时间】: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


    【解决方案1】:

    MorpheeException 构造函数中你可以find calling method using reflection 然后使用它。

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2014-05-30
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多