使用MethodInvoker时出现非Invocable错误

时间:2012-06-18 09:49:51

标签: c# invoke

我使用此代码:

 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'不能像方法一样使用。 我怎样才能解决这个问题?感谢。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

您可以这样称呼:

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