Esempio n. 1
0
        /// <summary>
        ///     将一个智能对象转换为二进制元数据
        /// </summary>
        /// <param name="obj">智能对象</param>
        /// <returns>返回二进制元数据</returns>
        /// <exception cref="PropertyNullValueException">字段相关的Attribute.IsRequire = true, 并且该字段的值为null</exception>
        /// <exception cref="UnexpectedValueException">不期待的结果异常,通常因为对Blob类型的取值 = null</exception>
        /// <exception cref="NotSupportedException">系统不支持的序列化类型</exception>
        /// <exception cref="DefineNoMeaningException">无意义的智能字段Attribute值</exception>
        /// <exception cref="MethodAccessException">类型权限定义错误</exception>
        /// <exception cref="Exception">内部错误</exception>
        public static byte[] ToBytes(IThriftObject obj)
        {
            if (obj == null)
            {
                return(null);
            }
            IMemorySegmentProxy proxy = null;

            try
            {
                proxy = MemorySegmentProxyFactory.Create();
                ToBytes(obj, proxy);
                //end flag of Thrift protocol object.
                proxy.WriteSByte(0x00);
                return(proxy.GetBytes());
            }
            catch (Exception ex)
            {
                if (proxy != null)
                {
                    proxy.Dispose();
                }
                throw;
            }
        }
Esempio n. 2
0
        /// <summary>
        ///   将一个IntellectObject数组转换成byte数组的形式
        /// </summary>
        /// <param name="value">值</param>
        /// <exception cref="UnexpectedValueException">结果错误</exception>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public static byte[] BindIntellectObjectArray(IntellectObject[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            MemoryAllotter.Instance.Initialize();
            IMemorySegmentProxy proxy = MemorySegmentProxyFactory.Create();

            proxy.WriteUInt32((uint)value.Length);
            for (int i = 0; i < value.Length; i++)
            {
                IntellectObject elementObj = value[i];
                if (elementObj == null)
                {
                    proxy.WriteUInt16(0);
                }
                else
                {
                    MemoryPosition currentPostion = proxy.GetPosition();
                    proxy.Skip(2U);
                    int length = IntellectObjectEngine.ToBytes(elementObj, proxy);
                    proxy.WriteBackUInt16(currentPostion, (ushort)length);
                }
            }
            return(proxy.GetBytes());
        }
Esempio n. 3
0
        /// <summary>
        ///     将指定类型的实例序列化为二进制数据
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="instance">实例对象</param>
        /// <returns>返回序列化后的二进制数据, 如果instance为null, 则返回null</returns>
        /// <exception cref="ArgumentNullException">type 参数不能为空</exception>
        /// <exception cref="NotSupportedException">不被支持的类型</exception>
        public static byte[] ToBytes(Type type, object instance)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (instance == null)
            {
                return(null);
            }
            MemoryAllotter.Instance.Initialize();
            IMemorySegmentProxy proxy = null;

            try
            {
                proxy = MemorySegmentProxyFactory.Create();
                ToBytes(type, instance, proxy);
                return(proxy.GetBytes());
            }
            catch
            {
                if (proxy != null)
                {
                    proxy.Dispose();
                }
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     第三方数据转换成元数据
        /// </summary>
        /// <param name="metadata">第三方数据对象</param>
        /// <returns>元数据的二进制表现形式</returns>
        public static byte[] ToBytes(ResourceBlock metadata)
        {
            if (metadata == null)
            {
                return(null);
            }
            MemoryAllotter.Instance.Initialize();
            IMemorySegmentProxy proxy = null;

            try
            {
                proxy = MemorySegmentProxyFactory.Create();
                ToBytes(metadata, proxy);
                return(proxy.GetBytes());
            }
            catch
            {
                if (proxy != null)
                {
                    proxy.Dispose();
                }
                throw;
            }
        }
        /// <summary>
        ///     将一个智能对象转换为二进制元数据
        /// </summary>
        /// <param name="obj">智能对象</param>
        /// <returns>返回二进制元数据</returns>
        /// <exception cref="PropertyNullValueException">字段相关的Attribute.IsRequire = true, 并且该字段的值为null</exception>
        /// <exception cref="UnexpectedValueException">不期待的结果异常,通常因为对Blob类型的取值 = null</exception>
        /// <exception cref="NotSupportedException">系统不支持的序列化类型</exception>
        /// <exception cref="DefineNoMeaningException">无意义的智能字段Attribute值</exception>
        /// <exception cref="MethodAccessException">类型权限定义错误</exception>
        /// <exception cref="Exception">内部错误</exception>
        public static byte[] ToBytes(IIntellectObject obj)
        {
            if (obj == null)
            {
                return(null);
            }
            MemoryAllotter.Instance.Initialize();
            IMemorySegmentProxy proxy = null;

            try
            {
                proxy = MemorySegmentProxyFactory.Create();
                ToBytes(obj, proxy);
                return(proxy.GetBytes());
            }
            catch
            {
                if (proxy != null)
                {
                    proxy.Dispose();
                }
                throw;
            }
        }