3

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

4

1 回答 1

1

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

BinaryFormatter 不是在不同设置之间传输数据的好选择。即使是单一设置,它也几乎无法工作:p

于 2013-06-16T20:27:43.637 回答