2010-10-01 72 views
8

C#如何解決:C#

「異常被調用的目標引發異常」每次我跑我的porgram我得到這個異常: alt text

但是,當我在調試模式下運行,有沒有例外,程序正常工作,我該怎麼辦?

注:我不使用的invoke()在項目的任何地方

編輯:好了,這是在細節中發現的代碼: 如果有人知道如何使用protoBuff,和知道這個問題....

************** Exception Text ************** 
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> ProtoBuf.ProtoException: Incorrect wire-type deserializing TimeSpan 
    at ProtoBuf.ProtoBcl.ProtoTimeSpan.DeserializeTicks(SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\ProtoBcl\ProtoTimeSpan.cs:line 80 
    at ProtoBuf.ProtoBcl.ProtoTimeSpan.DeserializeDateTime(SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\ProtoBcl\ProtoTimeSpan.cs:line 41 
    at ProtoBuf.Property.PropertyDateTimeString`1.DeserializeImpl(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyDateTimeString.cs:line 32 
    at ProtoBuf.Property.Property`2.Deserialize(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\Property.cs:line 150 
    at ProtoBuf.Serializer`1.Deserialize[TCreation](T& instance, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 568 
    at ProtoBuf.Serializer`1.DeserializeChecked[TCreation](T& instance, SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 400 
    at ProtoBuf.SerializerItemProxy`2.Deserialize(TActualClass& instance, SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerProxy.cs:line 86 
    at ProtoBuf.Serializer.Deserialize[T](SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 302 
    at ProtoBuf.Serializer.Deserialize[T](Stream source) in c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 289 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at ProtoBuf.Serializer.NonGeneric.Deserialize(Type type, Stream source) in c:\protobuf-net_fixed\trunk\protobuf-net\NonGeneric.cs:line 154 
    at ProtoBuf.Serializer.NonGeneric.TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, Getter`2 typeReader, Object& item) in c:\protobuf-net_fixed\trunk\protobuf-net\NonGeneric.cs:line 128 
    at AccessPoint.MainForm.getEventsList() in C:\Users\user\Desktop\accesspoint\AccessPoint\AccessPoint\MainForm.cs:line 97 
    at AccessPoint.MainForm.Form1_Load(Object sender, EventArgs e) in C:\Users\user\Desktop\accesspoint\AccessPoint\AccessPoint\MainForm.cs:line 18 
    at System.Windows.Forms.Form.OnLoad(EventArgs e) 
    at System.Windows.Forms.Form.OnCreateControl() 
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 
    at System.Windows.Forms.Control.CreateControl() 
    at System.Windows.Forms.Control.WmShowWindow(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
    at System.Windows.Forms.Form.WmShowWindow(Message& m) 
    at System.Windows.Forms.Form.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

行97:

int startIndex = count - 10, index = 0; 
       object obj; 
       while (Serializer.NonGeneric.TryDeserializeWithLengthPrefix(file, PrefixStyle.Base128, tag => 
       { 
        return index++ >= startIndex ? typeof(EventsWireFrame) : null; 
       }, out obj)) 
       { 
        EventsWireFrame evt = (EventsWireFrame)obj; 
        AddEventToTable(evt.eventDate, evt.eventType, evt.eventKeyNumber, evt.eventKeyName, evt.eventDoor, true); 

       } 

我弄不明白,怎麼了?我是否需要添加另一部分代碼?也許seraliztaion?

+5

首先點擊Details並查看完整的異常和堆棧跟蹤。 – 2010-10-01 11:12:39

+0

做邁克爾說的話。什麼是實際的錯誤? – 2010-10-01 11:14:17

+0

我添加了代碼,如果你想要完整的,只要告訴我。 – 2010-10-01 11:17:07

回答

4

您正在使用Protobuf反序列化的東西不明白。可能數據序列化使用另一個版本的程序集或數據不是由您首先序列化。 Google協議緩衝區可用於將對象的表示寫入流。您可以稍後反序列化流以重新創建對象。但是,以同樣的方式序列化和反序列化對象非常重要。如果你只是向反序列化中提供垃圾,你將會拋出奇怪的異常。發生

問題在MainForm.cs,線路97

如果你只有當你在釋放模式,那麼也許你正在嘗試反序列化文件位於二進制目錄和釋放模式運行出現錯誤文件已過期,也就是說,它包含您序列化的舊版數據的序列化數據。

+0

好的,我在主帖中增加了一個部分,這是對您的評論 – 2010-10-01 11:29:11

27

TargetInvocationException通過告訴你它在「方法調用」期間崩潰,通常通過something.Invoke掩蓋了真正的異常。

你要做的就是查看異常對象的InnerException屬性(TargetInvocationException對象),這會給你帶來的實際異常和更有用的堆棧跟蹤。

+1

內部異常有我需要的所有細節來解決這個問題,在我的情況下確實是'Invalid Cast Exception'。 – Niklas 2016-10-21 22:50:48