为什么我得到这样的异常,我该如何反序列化数据?注意 Deserialize(Stream serializationStream) 方法抛出异常。Uri 是正确的。该文件位于我的计算机中
public Structure DeserializeStructForXBAPApplication()
{
var uri = new Uri(@"myserialization.bin", UriKind.Relative);
var info = Application.GetContentStream(uri);
Debug.Assert(info != null, "info != null");
var final = (Structure)new BinaryFormatter().Deserialize(info.Stream);
return final;
}
抛出的异常是:
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Step into: Stepping over non-user code 'System.RuntimeType.CreateInstanceImpl'
Step into: Stepping over non-user code 'MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance'
Step into: Stepping over non-user code 'System.Windows.Markup.WpfXamlLoader.Load'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'System.Windows.Markup.XamlReader.LoadBaml'
Step into: Stepping over non-user code 'System.Windows.Application.LoadBamlStreamWithSyncInfo'
Step into: Stepping over non-user code 'System.Windows.Threading.DispatcherOperation.InvokeImpl'
Step into: Stepping over non-user code 'System.Threading.ExecutionContext.RunInternal'
Step into: Stepping over non-user code 'System.Windows.Threading.Dispatcher.LegacyInvokeImpl'
Step into: Stepping over non-user code 'System.Windows.Threading.Dispatcher.PushFrameImpl'
Step into: Stepping over non-user code 'System.Windows.Application.StartDispatcherInBrowser'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'System.Windows.Threading.DispatcherOperation.InvokeImpl'
Step into: Stepping over non-user code 'System.Threading.ExecutionContext.RunInternal'
Step into: Stepping over non-user code 'System.Windows.Threading.Dispatcher.LegacyInvokeImpl'
编辑 它被序列化为:
public void SerializationStruct(Structure struc)
{
const string path = @"C:\myserialization.bin";
//byte[] result;
//Structure final;
//using (var stream = new MemoryStream())
using (var stream = new FileStream(path, FileMode.Create))
{
new BinaryFormatter().Serialize(stream, struc);
stream.Flush();
stream.Close();
stream.Dispose();
//result = stream.ToArray();
}
}
并且可以使用函数调用来反序列化它
public Structure DeserializationStruct()
{
Structure final;
const string path = @"C:\myserialization.bin";
using (var rStream = new FileStream(path, FileMode.Open))
{
final = (Structure)new BinaryFormatter().Deserialize(rStream);
rStream.Close();
rStream.Dispose();
}
return final;
}
由 Winforms 应用程序调用。所以序列化本身不应该是我认为的问题,而是特权问题。