【问题标题】:Non-Invocable error when use MethodInvoker使用 MethodInvoker 时出现不可调用错误
【发布时间】:2012-06-18 09:49:51
【问题描述】:

我使用这个代码:

 MethodInvoker TileLoadCompleteMethodInvoker = delegate()
{
    CleanStatusLabelTimer.Enabled = true;
    MemoryTextBox.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0.00} MB of {1:0.00} MB", MainMapControl.Manager.MemoryCache.Size, MainMapControl.Manager.MemoryCache.Capacity);
};
try
{
    BeginInvoke(TileLoadCompleteMethodInvoker);
}
catch (TargetInvocationException ex)
{
    BLL.FunctionsClass.LogExceptions(ex.InnerException);
}

LogExceptions 方法在这里:

    public class LogExceptions
{
public static void WriteLogException(Exception exMsg)
{
    string filePath = Application.StartupPath + "\\ExceptionLog.log";
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, true))
    {
        file.WriteLine("-------------------Exception Begin----------------------");
        file.WriteLine(string.Format("Date: {0}, Time: {1}", DateTime.Now.ToShortTimeString()));
        file.WriteLine(string.Format("Exception Message: {0}", exMsg.ToString()));
        file.WriteLine(string.Format("Source: {0}", exMsg.Source));
        file.WriteLine("-------------------Exception End----------------------");
        file.WriteLine();
    }
}
}

此方法将异常保存在日志文件中。但是调用 LogExceptions 方法时发生此错误; 不可调用的成员 'IslamAtlas.BLL.FunctionsClass.LogExceptions' 不能像方法一样使用。 我怎样才能解决这个问题?谢谢。

【问题讨论】:

    标签: c# invoke


    【解决方案1】:

    你是在调用类,而不是方法!
    改用这个:

    BLL.FunctionsClass.LogExceptions.WriteLogException(ex.InnerException);
    

    更多:这里还有一个错误:

    string.Format("Date: {0}, Time: {1}", DateTime.Now.ToShortTimeString()
    

    你用两个参数调用String.Format,但只传递一个!

    越来越多:这条线

    string.Format("Exception Message: {0}", exMsg.ToString())
    

    应该是我的意见

    string.Format("Exception Message: {0}", exMsg.Message)
    

    【讨论】:

    • 我使用 InnerException 但不是真的并且发生了错误。 :D 我修复了 string.format。
    • @1mohammadi.ir:你看到我和 leppie 的答案了吗?您必须使用函数名称 WriteLogException 更改代码。
    【解决方案2】:

    你有这样的称呼:

    BLL.FunctionsClass.LogExceptions.WriteLogException(ex.InnerException);
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多