示例#1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ComSerializable{T}" /> class.
        /// </summary>
        /// <param name="info">The information.</param>
        /// <param name="context">The context.</param>
        protected ComSerializable(SerializationInfo info, StreamingContext context)
        {
            byte[] data   = (byte[])info.GetValue("DATA", typeof(byte[]));
            string progId = info.GetString("PROGID");

            using (ComReleaser com = new ComReleaser())
            {
                IMemoryBlobStream blob = new MemoryBlobStreamClass();
                com.ManageLifetime(blob);

                IMemoryBlobStreamVariant variant = (IMemoryBlobStreamVariant)blob;
                variant.ImportFromVariant(data);

                IObjectStream stream = new ObjectStreamClass();
                stream.Stream = blob;

                com.ManageLifetime(stream);

                Type t = Type.GetTypeFromProgID(progId);

                IPersistStream persist = (IPersistStream)Activator.CreateInstance(t);
                persist.Load(stream);

                this.Value = (T)persist;
            }
        }
示例#2
0
        /// <summary>
        ///     Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with the data needed to serialize the
        ///     target object.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to populate with data.</param>
        /// <param name="context">
        ///     The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this
        ///     serialization.
        /// </param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            using (ComReleaser cr = new ComReleaser())
            {
                IPersistStream persist = this.Value as IPersistStream;
                if (persist == null)
                {
                    return;
                }

                IObjectStream stream = new ObjectStreamClass();
                cr.ManageLifetime(stream);

                IMemoryBlobStream blob = new MemoryBlobStreamClass();
                cr.ManageLifetime(blob);

                stream.Stream = blob;

                persist.Save(stream, 0);

                IMemoryBlobStreamVariant variant = (IMemoryBlobStreamVariant)blob;

                object value;
                variant.ExportToVariant(out value);

                var data = (byte[])value;
                info.AddValue("DATA", data);
                info.AddValue("PROGID", this.ProgId);
            }
        }