示例#1
0
        /// <summary>
        /// Serializes binary data to a XmlNode.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ctorParamType"></param>
        /// <param name="parent"></param>
        protected void SerializeBinaryObject(Object obj, Type ctorParamType, XmlNode parent)
        {
            XmlElement proplist = null;
            String     val      = null;

            try
            {
                // If the objact is a Stream or can be converted to a byte[]...
                TypeConverter tc = TypeDescriptor.GetConverter(obj.GetType());
                if (tc.CanConvertTo(typeof(byte[])) || typeof(Stream).IsAssignableFrom(obj.GetType()))
                {
                    byte[] barr = null;

                    // Convert to byte[]
                    if (typeof(Stream).IsAssignableFrom(obj.GetType()))
                    {
                        // Convert a Stream to byte[]
                        var bctc = new BinaryContainerTypeConverter();
                        barr = bctc.ConvertStreamToByteArray((Stream)obj);
                    }
                    else
                    {
                        // Convert the object to a byte[]
                        barr = (byte[])tc.ConvertTo(obj, typeof(byte[]));
                    }

                    // Create a constructor node
                    proplist = parent.OwnerDocument.CreateElement(_taglib.CONSTRUCTOR_TAG);
                    parent.AppendChild(proplist);

                    // Set info about the constructor type as attributes
                    SetObjectInfoAttributes("0", ctorParamType, proplist);

                    // Create a node for the binary data
                    XmlNode bindata = proplist.OwnerDocument.CreateElement(_taglib.BINARY_DATA_TAG);
                    proplist.AppendChild(bindata);

                    // Set info about the binary data type as attributes (currently it's always byte[])
                    SetObjectInfoAttributes("0", typeof(byte[]), bindata);

                    // Convert the byte array to a string so it's easy to store it in XML
                    val = Convert.ToBase64String(barr, 0, barr.Length);

                    bindata.InnerText = val;
                }
            }
            catch (Exception exc)
            {
                if (!IgnoreSerialisationErrors)
                {
                    throw exc;
                }
                else
                {
                    // perhaps logging
                }
            }
        }
示例#2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="data"></param>
        public BinaryContainer(Stream data)
        {
            BinaryContainerTypeConverter conv = new BinaryContainerTypeConverter();

            this.data = conv.ConvertStreamToByteArray(data);
        }