【发布时间】:2013-06-16 20:05:27
【问题描述】:
为什么会出现这种异常,如何反序列化数据? 注意 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 应用程序调用。所以序列化本身不应该是我认为的问题,而是特权问题。
【问题讨论】:
-
它是如何序列化的?结构如何?
-
你在xbap中序列化了吗?还是您在 xbap 和“完整”.net 之间传输数据?如果是后者,我建议尝试不同的序列化程序; XmlSerializer 或 protobuf-net 可能
-
它没有在 XBAP 中序列化,实际上我 90% 确定它在某些 WinForms 应用程序中。 Struct 不是 XML 而是二进制数据
-
@lukasz.pek 该结构既不是 XML 也不是 BinaryFormatter 数据——它很简单:一个结构。然而,做你想做的事情,序列化是你需要的工具——但是 BinaryFormatter 在这里可能根本不起作用。
标签: c# .net serialization xbap