C#中二进制序列化到ResultBuffer

时间:2015-04-21 20:56:03

标签: c# serialization autocad binary-serialization

我有一个可用的XML Serializer,它将C#对象序列化为AutoCAD中的实体。我希望能够做同样的事情,但对于XML不起作用的情况使用二进制序列化。到目前为止,我的序列化方法如下所示:

public static void BinarySave(Entity entityToWriteTo, Object objToSerialize, string key = "default")
{
    using (MemoryStream stream = new MemoryStream())
    {
        BinaryFormatter serializer = new BinaryFormatter();
        serializer.Serialize(stream, objToSerialize);
        stream.Position = 0;

        ResultBuffer data = new ResultBuffer();

        /*Code to get binary serialization into result buffer*/

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            using (DocumentLock docLock = doc.LockDocument())
            {
                if (!entityToWriteTo.IsWriteEnabled)
                {
                    entityToWriteTo = tr.GetObject(entityToWriteTo.Id, OpenMode.ForWrite) as Entity;
                }

                if (entityToWriteTo.ExtensionDictionary == ObjectId.Null)
                {
                    entityToWriteTo.CreateExtensionDictionary();
                }

                using (DBDictionary dict = tr.GetObject(entityToWriteTo.ExtensionDictionary, OpenMode.ForWrite, false) as DBDictionary)
                {
                    Xrecord xrec;

                    if (dict.Contains(key))
                    {
                        xrec = tr.GetObject(dict.GetAt(key), OpenMode.ForWrite) as Xrecord;
                        xrec.Data = data;
                    }

                    else
                    {
                        xrec = new Xrecord();
                        xrec.Data = data;
                        dict.SetAt(key, xrec);
                        tr.AddNewlyCreatedDBObject(xrec, true);
                    }

                    xrec.Dispose();
                }

                tr.Commit();
            }

           data.Dispose();
        }
    }
}

它主要基于我的XML Serializer,除了我不知道如何将序列化对象放入resultbuffer以添加到entityToWriteTo的Xrecord中。

1 个答案:

答案 0 :(得分:1)

如果XML由于某种原因不适合您,我建议您尝试使用其他文本数据格式,例如JSON。免费和开源的JSON格式化程序Json.NET支持可以提升XmlSerializer的情况,包括

另外,JSON非常易读,因此您可以通过目视检查来诊断数据中的问题。

话虽这么说,您可以使用以下帮助方法将输出流从BinaryFormatter转换为base64字符串:

public static class BinaryFormatterHelper
{
    public static string ToBase64String<T>(T obj)
    {
        using (var stream = new MemoryStream())
        {
            new BinaryFormatter().Serialize(stream, obj);
            return Convert.ToBase64String(stream.GetBuffer(), 0, checked((int)stream.Length)); // Throw an exception on overflow.
        }
    }

    public static T FromBase64String<T>(string data)
    {
        using (var stream = new MemoryStream(Convert.FromBase64String(data)))
        {
            var formatter = new BinaryFormatter();
            var obj = formatter.Deserialize(stream);
            if (obj is T)
                return (T)obj;
            return default(T);
        }
    }
}

然后,生成的字符串可以存储在ResultBuffer中,就像存储XML字符串一样。