简体   繁体   English

在C#中对ResultBuffer进行二进制序列化

[英]Binary Serialization to ResultBuffer in C#

I have a working XML Serializer which serializes a C# object to an entity in AutoCAD. 我有一个工作的XML序列化程序,该序列化程序将C#对象序列化为AutoCAD中的实体。 I'd like to be able to do the same thing but with Binary Serialization for the cases in which XML does not work. 我希望能够做同样的事情,但是对于XML不起作用的情况,可以使用二进制序列化。 So far my serialization method looks like this: 到目前为止,我的序列化方法如下所示:

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();
        }
    }
}

It's heavily based on my XML Serializer except I have no idea how to get the serialized object into a resultbuffer to be added to the Xrecord of entityToWriteTo. 它很大程度上基于我的XML序列化器,除了我不知道如何将序列化的对象放入要添加到entityToWriteTo的Xrecord的结果缓冲区中。

If XML isn't working for you for some reason, I'd suggest trying a different textual data format such as JSON. 如果XML由于某种原因无法为您服务,我建议您尝试使用其他文本数据格式,例如JSON。 The free and open-source JSON formatter Json.NET has some support for situations that can trip up XmlSerializer , including 免费且开源的JSON格式化程序Json.NET对可能导致XmlSerializer 崩溃的情况提供了一些支持,包括

Plus, JSON is quite readable so you may be able to diagnose problems in your data by visual examination. 另外,JSON可读性强,因此您可以通过目视检查来诊断数据中的问题。

That being said, you can convert the output stream from BinaryFormatter to a base64 string using the following helper methods: 话虽如此,您可以使用以下辅助方法将输出流从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);
        }
    }
}

The resulting string can then be stored in a ResultBuffer as you would store an XML string. 然后可以将结果字符串存储在ResultBuffer就像存储XML字符串一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM