Ejemplo n.º 1
0
        public byte[] Serialize(object data, out long length)
        {
            Contract.Ensures(Contract.Result <byte[]>() != null);
            Contract.Ensures(Contract.Result <byte[]>().Length >= Contract.ValueAtReturn(out length));

            using (var memory = new MemoryStream())
            {
                if (data == null)
                {
                    memory.WriteByte(1);
                }
                else
                {
                    memory.WriteByte(0);

                    new SecurityPermission(SecurityPermissionFlag.SerializationFormatter).Assert();

                    try
                    {
                        formatter.Serialize(memory, data);
                    }
                    finally
                    {
                        CodeAccessPermission.RevertAssert();
                    }
                }

                length = memory.Length;

                return(memory.GetBuffer());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 把对象序列化转换成字符串,默认为二进制序列化
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="formatterType"></param>
        /// <returns></returns>

        public static string SerializeObjectToString(object graph, FormatterType formatterType)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                IRemotingFormatter formatter = GetFormatter(formatterType);
                formatter.Serialize(memoryStream, graph);
                Byte[] arrGraph = memoryStream.ToArray();
                return(Convert.ToBase64String(arrGraph));
            }
        }
Ejemplo n.º 3
0
 private string DictionaryToBase64String(IDictionary <string, object> values)
 {
     using (var memStream = new MemoryStream())
     {
         memStream.Seek(0, SeekOrigin.Begin);
         _formatter.Serialize(memStream, values);
         memStream.Seek(0, SeekOrigin.Begin);
         var bytes = memStream.ToArray();
         return(Convert.ToBase64String(bytes));
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Serializes the specified object to serialize.
        /// </summary>
        /// <typeparam name="TObjectType">Type of the object to serialize.</typeparam>
        /// <param name="objectToSerialize">The object to serialize.</param>
        /// <returns>Serialized bytes of the object</returns>
        public byte[] Serialize <TObjectType>(TObjectType objectToSerialize)
        {
            byte[] serializedObject;

            using (var memStream = new MemoryStream())
            {
                _formatter.Serialize(memStream, objectToSerialize);
                serializedObject = memStream.ToArray();
            }

            return(serializedObject);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 把对象序列化转换为字符串
        /// </summary>
        /// <param name="graph">可串行化对象实例</param>
        /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param>
        /// <returns>串行化转化结果</returns>
        /// <remarks>调用BinaryFormatter或SoapFormatter的Serialize方法实现主要转换过程。
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\SerializationHelperTest.cs" region="TestSerializeAndDeserialize" lang="cs" title="把对象转换为字符串" />
        /// </remarks>
        public static string SerializeObjectToString(this object graph, SerializationFormatterType formatterType)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph");

            using (MemoryStream memoryStream = new MemoryStream())
            {
                IRemotingFormatter formatter = GetFormatter(formatterType);
                formatter.Serialize(memoryStream, graph);
                Byte[] arrGraph = memoryStream.ToArray();

                return(Convert.ToBase64String(arrGraph));
            }
        }
Ejemplo n.º 6
0
        public static string SerializeObject(object graph, SerializationFormatterType formatterType)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }

            if (SerializationFormatterType.Xml == formatterType)
            {
                return(XmlSerializeObject(graph));
            }

            using (MemoryStream memoryStream = new MemoryStream())
            {
                IRemotingFormatter formatter = GetFormatter(formatterType);
                formatter.Serialize(memoryStream, graph);
                byte[] arrGraph = memoryStream.ToArray();
                return(Convert.ToBase64String(arrGraph));
            }
        }
Ejemplo n.º 7
0
        public static bool SerializeObjectToFile(object graph, SerializationFormatterType formatterType, string path)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }

            if (SerializationFormatterType.Xml == formatterType)
            {
                return(XmlSerializeObjectToFile(graph, path));
            }

            using (FileStream fileStream = new FileStream(path, FileMode.Create))
            {
                IRemotingFormatter formatter = GetFormatter(formatterType);
                formatter.Serialize(fileStream, graph);
                fileStream.Flush();
                return(true);
            }
        }