简体   繁体   English

C#序列化-无效的强制转换异常

[英]C# Serialization - Invalid Cast Exception

I've got some Issues with my Windows Forms Program. 我的Windows窗体程序存在一些问题。 To save some variables I use this method with serialization. 为了保存一些变量,我将此方法与序列化一起使用。 It worked fine, but now somehow it won't anymore... When I run the following code, I always get a Invalid Cast Exception. 它工作正常,但是现在不知何故了……当我运行以下代码时,总是会收到无效的强制转换异常。 (I'll post the error message at the bottom). (我将在底部显示错误消息)。

The thing is, that there actually shouldnt be any invalid cast. 问题是,实际上不应该有任何无效的转换。 I checked the one called in the error message, but the cast there is correct. 我检查了错误消息中调用的那个,但是那里的演员表是正确的。

using (FileStream fs = File.Open(@"data", FileMode.Open, FileAccess.Read))
                    {
                        BinaryFormatter bf = new BinaryFormatter(); ;
                        Form1.Money = (ulong)(bf.Deserialize(fs));
                        Form1.LifetimeMoney = (ulong)(bf.Deserialize(fs));
                        // Lade Achievement Variablen aus Form1
                        Form1.lifetime1 = (int)(bf.Deserialize(fs));
                        Form1.lifetime_booked = (int)(bf.Deserialize(fs));
                        Form1.lifetime1_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.lifetime2 = (int)(bf.Deserialize(fs));
                        Form1.lifetime2_booked = (int)(bf.Deserialize(fs));
                        Form1.lifetime2_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.lifetime3 = (int)(bf.Deserialize(fs));
                        Form1.lifetime3_booked = (int)(bf.Deserialize(fs));
                        Form1.lifetime3_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.farms50 = (int)(bf.Deserialize(fs));
                        Form1.farms50_booked = (int)(bf.Deserialize(fs));
                        Form1.farms50_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.farms500 = (int)(bf.Deserialize(fs));
                        Form1.farms500_booked = (int)(bf.Deserialize(fs));
                        Form1.farms500_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.shops50 = (int)(bf.Deserialize(fs));
                        Form1.shops50_booked = (int)(bf.Deserialize(fs));
                        Form1.shops50_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.shops500 = (int)(bf.Deserialize(fs));
                        Form1.shops500_booked = (int)(bf.Deserialize(fs));
                        Form1.shops500_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.factory50 = (int)(bf.Deserialize(fs));
                        Form1.factory50_booked = (int)(bf.Deserialize(fs));
                        Form1.factory50_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.factory500 = (int)(bf.Deserialize(fs));
                        Form1.factory500_booked = (int)(bf.Deserialize(fs));
                        Form1.factory500_cchanged = (bool)(bf.Deserialize(fs));
                        Form1.zinsAmount = (int)(bf.Deserialize(fs));
                        // Lade various
                        Form1.timePlayed = (long)(bf.Deserialize(fs));
                        Form1.loadedGame = (int)(bf.Deserialize(fs));
                        // Lade Businesstrip Daten
                        Form1.businessTimer1 = (int)(bf.Deserialize(fs));
                        Form1.business1Random = (int)(bf.Deserialize(fs));
                        Form1.business1Income = (int)(bf.Deserialize(fs));
                        Form1.triptime1 = (double)(bf.Deserialize(fs));
                        Form1.businessLifetimeEarning = (ulong)(bf.Deserialize(fs));
                        // Lade Casino Daten aus Form1
                        Form1.inv1chance = (int)(bf.Deserialize(fs));
                        Form1.inv2chance = (int)(bf.Deserialize(fs));
                        Form1.investment1 = (int)(bf.Deserialize(fs));
                        Form1.investment2 = (char)(bf.Deserialize(fs));
                        Form1.moneyWonCasino = (ulong)(bf.Deserialize(fs));
                        fs.Close();
                    } // using end
                }

Error Message: 错误信息:

************** Exception Text **************
System.InvalidCastException: Specified cast is not valid.
   at IdleGameTest.LoadSave.LoadGame() in c:\Users\Sior\Documents\Visual Studio 2013\Projects\IdleGameTest\IdleGameTest\LoadSave.cs:line 176
   at IdleGameTest.Form1.b_loadGame_Click(Object sender, EventArgs e) in c:\Users\Sior\Documents\Visual Studio 2013\Projects\IdleGameTest\IdleGameTest\Form1.cs:line 341
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.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)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
IdleGameTest
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Sior/Documents/Visual%20Studio%202013/Projects/IdleGameTest/IdleGameTest/bin/Debug/IdleGameTest.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

The following: System.InvalidCastException: Specified cast is not valid. 以下内容:System.InvalidCastException:指定的强制转换无效。 at IdleGameTest.LoadSave.LoadGame() in c:\\Users\\Sior\\Documents\\Visual Studio 2013\\Projects\\IdleGameTest\\IdleGameTest\\LoadSave.cs:line 176 在C:\\ Users \\ Sior \\ Documents \\ Visual Studio 2013 \\ Projects \\ IdleGameTest \\ IdleGameTest \\ LoadSave.cs:IdleGameTest.LoadSave.LoadGame()中:第176行

references to Form1.investment1 = (int)(bf.Deserialize(fs)); 引用Form1.investment1 =(int)(bf.Deserialize(fs)); which is a correct cast, since Form1.investment1 is a integer. 这是正确的转换,因为Form1.investment1是整数。 So I don't get it. 所以我不明白。

I really could need some help here, is there something obvious I just don't see? 我真的需要这里的帮助,有什么明显的我看不到的东西吗?

It sounds like your file is incorrectly formatted, or you're missing a property along the way. 听起来您的文件格式不正确,或者在此过程中缺少属性。 You say 你说

references to Form1.investment1 = (int)(bf.Deserialize(fs)); 引用Form1.investment1 =(int)(bf.Deserialize(fs)); which is a correct cast, since Form1.investment1 is a integer. 这是正确的转换,因为Form1.investment1是整数。

But Form1.investment1 is irrelevant. 但是Form1.investment1无关紧要。 An invalid cast expresses an error, in most cases, on the right side of the equals, where the cast is actually occurring. 在大多数情况下,无效的转换表示错误,在等号的右侧(实际发生转换)。 bf.Deserialize(fs) is returning something that's not an int , so it can't be cast to an int . bf.Deserialize(fs)返回的不是int ,因此无法将其bf.Deserialize(fs)转换为int It isn't that it can't set the property to that, since that would be a compiler error. 不是因为它不能设置该属性,因为那将是编译器错误。

Check what bf.Deserialize(fs) is returning at that point and I bet you'll find your problem. 检查那时bf.Deserialize(fs)返回什么,我敢打赌,您会发现问题所在。

Are you sure the properties you're deserializing match in order and quantity to those which you serialized to build the file in the first place? 您确定要反序列化的属性在顺序和数量上与首先序列化以生成文件的属性相匹配吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM