Esempio n. 1
0
        /// <summary>
        /// 将目标对象属性转换为键值列表
        /// </summary>
        /// <param name="targetObject"></param>
        /// <param name="recursion"></param>
        /// <returns></returns>
        public static Dictionary <string, object> ToDictionary(object targetObject, bool recursion = true)
        {
            if (targetObject == null)
            {
                return(null);
            }
            //
            var    type       = targetObject.GetType();
            var    ps         = PropertyAccessor.GetGets(targetObject);
            var    dictionary = new Dictionary <string, object>(ps.Length);
            object value;

            for (int i = 0, l = ps.Length; i < l; i++)
            {
                value = PropertyAccessor.GetValue(targetObject, ps[i].PropertyInfo.Name);
                if (value == null || !recursion || value is ValueType || value is string || value is IDictionary || value is ICollection)
                {
                    dictionary.Add(ps[i].PropertyInfo.Name, value);
                }
                else
                {
                    dictionary.Add(ps[i].PropertyInfo.Name, ToDictionary(value, recursion));
                }
            }

            return(dictionary);
        }
Esempio n. 2
0
        /// <summary>
        /// 将对象序列化成字节数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected virtual byte[] SerializeObject(object value)
        {
            //all length
            //, item 1 name size, item 1 name data, item 1 value size, item 1 value data
            //, item 2 name size,  item 2 name data, item 2 value size, item 2 value data
            //, item N name size,  item N name data, item N value size, item N value data
            //
            var    ps = PropertyAccessor.GetGets(value.GetType());
            object itemValue;

            //
            byte[] nameBuffer, valueBuffer, sizeBuffer;
            int    length   = 0;
            int    psLength = ps.Length;

            using (var m = new MemoryStream())
            {
                //all length
                sizeBuffer = BaseDataConverter.ToBytes(psLength);
                m.Write(sizeBuffer, 0, SIZE_INT32);
                //
                for (int i = 0; i < psLength; i++)
                {
                    //name
                    nameBuffer = Encoding.UTF8.GetBytes(ps[i].PropertyInfo.Name);
                    length     = nameBuffer.Length;
                    sizeBuffer = BaseDataConverter.ToBytes(length);

                    m.Write(sizeBuffer, 0, SIZE_INT32);
                    m.Write(nameBuffer, 0, length);

                    //value
                    itemValue   = PropertyAccessor.GetValue(value, ps[i].PropertyInfo.Name);
                    valueBuffer = this.Serialize(itemValue);
                    if (valueBuffer == null)
                    {
                        sizeBuffer = BaseDataConverter.ToBytes(-1);
                        m.Write(sizeBuffer, 0, SIZE_INT32);
                        //no body
                    }
                    else
                    {
                        length     = valueBuffer.Length;
                        sizeBuffer = BaseDataConverter.ToBytes(length);
                        m.Write(sizeBuffer, 0, SIZE_INT32);
                        m.Write(valueBuffer, 0, length);
                    }
                }

                return(m.ToArray());
            }
        }