我的Windows窗体程序存在一些问题。为了保存一些变量,我使用此方法进行序列化。它工作得很好,但现在它不知何故再也不会...... 当我运行以下代码时,我总是得到一个无效的强制转换异常。 (我将在底部发布错误消息)。
事实是,实际上不应该有任何无效的演员表。我检查了错误消息中调用的那个,但是那里的强制转换是正确的。
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
}
错误讯息:
************** 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
----------------------------------------
以下内容: System.InvalidCastException:指定的强制转换无效。 at IdleGameTest.LoadSave.LoadGame()in c:\ Users \ Sior \ Documents \ Visual Studio 2013 \ Projects \ IdleGameTest \ IdleGameTest \ LoadSave.cs:第176行
对Form1.investment1 =(int)的引用(bf.Deserialize(fs));这是一个正确的演员,因为 Form1.investment1是一个整数。所以我不明白。
我真的需要一些帮助,有什么显而易见的我不知道吗?
答案 0 :(得分:1)
听起来您的文件格式不正确,或者您在途中错过了某个属性。你说
对Form1.investment1 =(int)的引用(bf.Deserialize(fs));这是一个正确的强制转换,因为Form1.investment1是一个整数。
但Form1.investment1
无关紧要。在大多数情况下,无效的强制转换表示在等于实际发生强制转换的等号右侧的错误。 bf.Deserialize(fs)
返回的内容不是int
,因此无法转换为int
。它不能将属性设置为,因为那将是编译器错误。
检查当时bf.Deserialize(fs)
返回的内容,我打赌你会发现问题。
您确定要反序列化的属性在顺序和数量上与首先构建文件的序列号相匹配吗?