gpt4 book ai didi

c# - 如何在 XBAP 应用程序中使用 BinaryFormatter 反序列化流?

转载 作者:行者123 更新时间:2023-11-30 17:53:37 25 4
gpt4 key购买 nike

为什么我会得到这样的异常,我该如何反序列化数据?注意 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 应用程序调用。所以序列化本身应该不是我觉得的问题,而是权限的问题。

最佳答案

BinaryFormatter 是一个字段级序列化程序。它可能无法在沙箱中运行也就不足为奇了。我建议您只需尝试不同的序列化程序 - XmlSerializer 将是一个好的开始,或者 protobuf-net 可能。

BinaryFormatter 不是在不同设置之间传输数据的好选择。它甚至在单个设置中也几乎无法工作:p

关于c# - 如何在 XBAP 应用程序中使用 BinaryFormatter 反序列化流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17137197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com