示例#1
0
        /** <inheritDoc /> */
        public T ToBinary <T>(object obj)
        {
            if (obj is IBinaryObject)
            {
                return((T)obj);
            }

            using (var stream = new BinaryHeapStream(1024))
            {
                // Serialize.
                BinaryWriter writer = _marsh.StartMarshal(stream);

                try
                {
                    writer.Write(obj);
                }
                finally
                {
                    // Save metadata.
                    _marsh.FinishMarshal(writer);
                }

                // Deserialize.
                stream.Seek(0, SeekOrigin.Begin);

                return(_marsh.Unmarshal <T>(stream, BinaryMode.ForceBinary));
            }
        }