コード例 #1
0
        public static object UnPack(Type type, byte[] datas, ref int offset)
        {
            if (type == typeof(object))
            {
                throw new Exception("类型不能为object:" + type);
                type = typeof(string);
            }
            object obj     = null;
            var    lenData = new byte[4];

            if (datas == null || datas.Length == 0)
            {
                return(null);
            }
            Buffer.BlockCopy(datas, offset, lenData, 0, lenSaveLength);

            int len = BitConverter.ToInt32(lenData, 0);

            offset += lenSaveLength;
            if (len > 0)
            {
                type = ReturnType(type);

                byte[] data = new byte[len];
                Buffer.BlockCopy(datas, offset, data, 0, len);
                offset += len;

                if (type.BaseType == typeof(Enum))
                {
                    type = Enum.GetUnderlyingType(type);
                }
                var a = methods.TryGetValue(type, out Tuple <toByte, fromByte> method);
                if (a)
                {
                    return(method.Item2(type, data));
                }
                if (type.IsGenericType || type.IsArray)
                {
                    if (typeof(System.Collections.IDictionary).IsAssignableFrom(type))
                    {
                        return(DicFormat.UnPack(type, data));
                    }
                    else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type))
                    {
                        return(ListFormat.UnPack(type, data));
                    }
                }
                return(ClassFormat.UnPack(type, data));
            }
            return(obj);
        }
コード例 #2
0
ファイル: FieldFormat.cs プロジェクト: zyj0021/CRL5
        public static byte[] Pack(Type type, object param)
        {
            type = ReturnType(type);

            var len = 0;

            byte[] data = null;

            if (param == null)
            {
                len = 0;
            }
            else
            {
                if (type == typeof(object))//object转为string
                {
                    throw new Exception("类型不能为object:" + param);
                    type = typeof(string);
                    if (param != null)
                    {
                        param = param.ToString();
                    }
                    else
                    {
                        param = "";
                    }
                }
                if (param is Enum)
                {
                    type = Enum.GetUnderlyingType(param.GetType());
                }
                var a = methods.TryGetValue(type, out Tuple <toByte, fromByte> method);
                if (a)
                {
                    data = method.Item1(type, param);
                }
                else
                {
                    if (type.IsGenericType || type.IsArray)
                    {
                        if (typeof(System.Collections.IDictionary).IsAssignableFrom(type))
                        {
                            data = DicFormat.Pack(param);
                        }
                        else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type))
                        {
                            data = ListFormat.Pack(param);
                        }
                        else
                        {
                            data = ClassFormat.Pack(type, param);
                        }
                    }
                    else
                    {
                        data = ClassFormat.Pack(type, param);
                    }
                }

                if (data != null)
                {
                    len = data.Length;
                }
            }
            var lenData = BitConverter.GetBytes(len);
            var datas   = new byte[lenSaveLength + len];

            Buffer.BlockCopy(lenData, 0, datas, 0, lenSaveLength);
            if (len > 0)
            {
                Buffer.BlockCopy(data, 0, datas, lenSaveLength, data.Length);
            }
            return(datas);
        }