"对象引用未设置为对象的实例"二进制反序列化

时间:2016-12-19 11:20:52

标签: c# sqlite serialization nullreferenceexception binaryformatter

我正在序列化一个类对象,并将其作为BLOB从C#应用程序存储在sqlite数据库中。

用于序列化的函数:public static byte[] ObjectToByteArray (AnalyzeSubstring obj)

在其他应用程序中,通过反序列化获取blob数据库并将blob数据库转换为对象。 反序列化的功能是:public static AnalyzeSubstring ByteArrayToObject (byte[] arrBytes)

public static byte[] ObjectToByteArray (AnalyzeSubstring obj)
              {
              if ( obj == null )
                  return null;
              BinaryFormatter bf = new BinaryFormatter ();
              //bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
              MemoryStream ms = new MemoryStream ();
              bf.Serialize (ms, obj);
              return ms.ToArray ();
              }

          public static AnalyzeSubstring ByteArrayToObject (byte[] arrBytes)
              {
              MemoryStream memStream = new MemoryStream ();
              BinaryFormatter binForm = new BinaryFormatter ();
              memStream.Write (arrBytes, 0, arrBytes.Length);
              memStream.Seek (0, SeekOrigin.Begin);
              AnalyzeSubstring obj = (AnalyzeSubstring) binForm.Deserialize (memStream);
              return obj;
              }

问题:反序列化AnalyzeSubstring obj = (AnalyzeSubstring) binForm.Deserialize (memStream);时符合要求。

失败并显示错误

  

对象引用未设置为对象的实例。

但是如果我们只在同一个应用程序中进行这两项操作,那么这项工作正常。

请帮忙

0 个答案:

没有答案
相关问题