2012-06-18 34 views
0

我用这个代码:非Invocable的错误时使用MethodInvoker

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

回答

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) 
+0

我使用的InnerException,但不正确的,错误happend。 :D我修正string.format。 –

+0

@ 1mohammadi.ir:你看到我和leppie的答案了吗?您**必须**用函数名称* WriteLogException *更改您的代码。 – Marco

+0

谢谢。这是一个愚蠢的问题,感谢很多人。 –

0

你把它想:

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