简体   繁体   中英

C# Serialization - Invalid Cast Exception

I've got some Issues with my Windows Forms Program. 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. at IdleGameTest.LoadSave.LoadGame() in c:\\Users\\Sior\\Documents\\Visual Studio 2013\\Projects\\IdleGameTest\\IdleGameTest\\LoadSave.cs:line 176

references to Form1.investment1 = (int)(bf.Deserialize(fs)); which is a correct cast, since Form1.investment1 is a integer. 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)); which is a correct cast, since Form1.investment1 is a integer.

But Form1.investment1 is irrelevant. 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 . 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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