Exemplo n.º 1
0
        public static Tuple <EntityBufType, bool> GetTypeBufType(Type tp)
        {
            if (tp == null)
            {
                return(null);
            }
            int key = tp.GetHashCode();

            if (TypeBufTypeDic.ContainsKey(key))
            {
                return(TypeBufTypeDic[key]);
            }
            else
            {
                try
                {
                    TypeBufTypeDicLockSlim.EnterWriteLock();
                    bool          isArray;
                    EntityBufType objType = MapBufType(tp, out isArray);
                    Tuple <EntityBufType, bool> touple = new Tuple <EntityBufType, bool>(objType, isArray);
                    try
                    {
                        TypeBufTypeDic.Add(key, touple);
                    }
                    catch
                    {
                    }
                    return(touple);
                }
                finally
                {
                    TypeBufTypeDicLockSlim.ExitWriteLock();
                }
            }
        }
Exemplo n.º 2
0
        public static List <Tuple <EntityBufType, bool> > GetTypeEntityBufType(Type tp)
        {
            if (tp == null)
            {
                return(null);
            }

            int key = tp.GetHashCode();

            try
            {
                //EntityBufTypeDicLockSlim.EnterUpgradeableReadLock();
                List <Tuple <EntityBufType, bool> > val;
                if (EntityBufTypeDic.TryGetValue(key, out val))
                {
                    if (val != null)
                    {
                        return(val);
                    }
                }

                try
                {
                    EntityBufTypeDicLockSlim.EnterWriteLock();

                    List <Tuple <EntityBufType, bool> > list = new List <Tuple <EntityBufType, bool> >();

                    PropertyInfo[] props   = tp.GetProperties();
                    bool           isArray = false;
                    foreach (PropertyInfo prop in props)
                    {
                        EntityBufType buftype = MapBufType(prop.PropertyType, out isArray);
                        buftype.Property = new PropertyInfoEx(prop);
                        list.Add(new Tuple <EntityBufType, bool>(buftype, isArray));
                    }
                    //再次检查下
                    try
                    {
                        EntityBufTypeDic.Add(key, list);
                    }
                    catch
                    {
                    }

                    return(list);
                }
                finally
                {
                    EntityBufTypeDicLockSlim.ExitWriteLock();
                }
            }
            finally
            {
                //EntityBufTypeDicLockSlim.ExitUpgradeableReadLock();
            }
        }
Exemplo n.º 3
0
        private static void SerializeComplex(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter msWriter)
        {
            if (isArray)
            {
                var vals = (object[])val;
                int len  = -1;
                if (vals != null)
                {
                    len = vals.Length;
                }
                msWriter.WriteInt32(len);
                if (len > 0)
                {
                    foreach (object v in vals)
                    {
                        //写入标志
                        if (v != null)
                        {
                            EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                            msWriter.WriteByte((byte)flag);
                            Serialize(v, msWriter);
                        }
                        else
                        {
                            EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                            msWriter.WriteByte((byte)flag);
                        }
                    }
                }
            }
            else
            {
                //msWriter.WriteByte(complexchar);
                //msWriter.WriteByte(complexchar);

                if (val != null)
                {
                    EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                    msWriter.WriteByte((byte)flag);
                    Serialize(val, msWriter);
                }
                else
                {
                    EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                    msWriter.WriteByte((byte)flag);
                }
            }
        }
Exemplo n.º 4
0
        private static object DeserializeSimple(EntityBufType buftype, bool isArray, MemoryStreamReader msReader)
        {
            if (buftype.EntityType == EntityType.COMPLEX)
            {
                throw new EntityBufException("无法反序列化复杂类型");
            }

            if (buftype.EntityType == EntityType.UNKNOWN)
            {
                throw new EntityBufException("无法反序列化未知类型");
            }

            switch (buftype.EntityType)
            {
            case EntityType.BYTE:
                if (isArray)
                {
                    return(msReader.ReadByteArray());
                }
                else
                {
                    return(msReader.ReadByte());
                }

            case EntityType.STRING:
                if (isArray)
                {
                    return(msReader.ReadStringArray());
                }
                else
                {
                    return(msReader.ReadString());
                }

            case EntityType.CHAR:
                if (isArray)
                {
                    return(msReader.ReadCharArray());
                }
                else
                {
                    return(msReader.ReadChar());
                }

            case EntityType.SHORT:
            case EntityType.INT16:
                if (isArray)
                {
                    return(msReader.ReadInt16Array());
                }
                else
                {
                    return(msReader.ReadInt16());
                }

            case EntityType.USHORT:
                if (isArray)
                {
                    return(msReader.ReadUInt16Array());
                }
                else
                {
                    return(msReader.ReadUInt16());
                }

            case EntityType.INT32:
                if (isArray)
                {
                    return(msReader.ReadInt32Array());
                }
                else
                {
                    return(msReader.ReadInt32());
                }

            case EntityType.INT64:
                if (isArray)
                {
                    return(msReader.ReadInt64Array());
                }
                else
                {
                    return(msReader.ReadInt64());
                }

            case EntityType.DOUBLE:
                if (isArray)
                {
                    return(msReader.ReadDoubleArray());
                }
                else
                {
                    return(msReader.ReadDouble());
                }

            case EntityType.FLOAT:
                if (isArray)
                {
                    return(msReader.ReadFloatArray());
                }
                else
                {
                    return(msReader.ReadFloat());
                }

            case EntityType.DECIMAL:
                if (isArray)
                {
                    return(msReader.ReadDeciamlArray());
                }
                else
                {
                    return(msReader.ReadDecimal());
                }

            case EntityType.DATETIME:
                if (isArray)
                {
                    return(msReader.ReadDateTimeArray());
                }
                else
                {
                    return(msReader.ReadDateTime());
                }

            case EntityType.BOOL:
                if (isArray)
                {
                    return(msReader.ReadBoolArray());
                }
                else
                {
                    return(msReader.ReadBool());
                }

            case EntityType.ENUM:
                if (isArray)
                {
                    string[] strarray = msReader.ReadStringArray();
                    Array    arr      = (Array)Activator.CreateInstance(buftype.ValueType, strarray.Length);
                    for (int i = 0; i < strarray.Length; i++)
                    {
                        arr.SetValue(Enum.Parse(buftype.ClassType, strarray[i]), i);
                    }
                    return(arr);
                }
                else
                {
                    return(Enum.Parse(buftype.ClassType, msReader.ReadString()));
                }

            case EntityType.DICTIONARY:
                if (isArray)
                {
                    int arrlen = msReader.ReadInt32();
                    if (arrlen == -1)
                    {
                        return(null);
                    }

                    var dicarr = (Array)Activator.CreateInstance(buftype.ValueType, arrlen);
                    for (int i = 0; i < arrlen; i++)
                    {
                        dicarr.SetValue(DeSerialize(buftype.ClassType, msReader), i);
                    }

                    return(dicarr);
                }
                else
                {
                    int dicLen = msReader.ReadInt32();
                    if (dicLen == -1)
                    {
                        return(null);
                    }

                    IDictionary idic         = (IDictionary)Activator.CreateInstance(buftype.ValueType);
                    var         keyvaluetype = GetDirctionaryKeyValueType(buftype.ValueType);

                    for (int i = 0; i < dicLen; i++)
                    {
                        idic.Add(DeSerialize(keyvaluetype[0], msReader), DeSerialize(keyvaluetype[1], msReader));
                    }

                    return(idic);
                }

            case EntityType.LIST:
                if (isArray)
                {
                    var listarrlen = msReader.ReadInt32();
                    if (listarrlen == -1)
                    {
                        return(null);
                    }
                    var listArray = (Array)Activator.CreateInstance(buftype.ValueType, listarrlen);
                    for (int i = 0; i < listarrlen; i++)
                    {
                        listArray.SetValue(DeSerialize(buftype.ClassType, msReader), i);
                    }
                    return(listArray);
                }
                else
                {
                    var listlen = msReader.ReadInt32();
                    if (listlen == -1)
                    {
                        return(null);
                    }
                    var list          = (IList)Activator.CreateInstance(buftype.ValueType);
                    var listvaluetype = GetListValueType(buftype.ValueType);
                    for (int i = 0; i < listlen; i++)
                    {
                        list.Add(DeSerialize(listvaluetype, msReader));
                    }
                    return(list);
                }

            case EntityType.ARRAY:
                if (isArray)
                {
                    var listarrlen = msReader.ReadInt32();
                    if (listarrlen == -1)
                    {
                        return(null);
                    }
                    var listArray = (Array)Activator.CreateInstance(buftype.ValueType, listarrlen);
                    for (int i = 0; i < listarrlen; i++)
                    {
                        listArray.SetValue(DeSerialize(buftype.ClassType, msReader), i);
                    }
                    return(listArray);
                }
                else
                {
                    var arrlen = msReader.ReadInt32();
                    if (arrlen == -1)
                    {
                        return(null);
                    }
                    var arr           = (Array)Activator.CreateInstance(buftype.ValueType, arrlen);
                    var listvaluetype = GetListValueType(buftype.ValueType);
                    for (int i = 0; i < arrlen; i++)
                    {
                        arr.SetValue(DeSerialize(listvaluetype, msReader), i);
                    }
                    return(arr);
                }

            default:
                throw new EntityBufException("反序列化错误");
            }
        }
Exemplo n.º 5
0
        private static void SerializeSimple(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter msWriter)
        {
            //if (bufType.EntityType == EntityType.COMPLEX)
            //{
            //    throw new Exception("无法序列化复杂类型");
            //}

            switch (bufType.EntityType)
            {
            case EntityType.BYTE:
                if (isArray)
                {
                    msWriter.WriteByteArray((byte[])val);
                }
                else
                {
                    msWriter.WriteByte((byte)val);
                }
                break;

            case EntityType.STRING:
                if (isArray)
                {
                    msWriter.WriteStringArray((string[])val);
                }
                else
                {
                    msWriter.WriteString((string)val);
                }
                break;

            case EntityType.SHORT:
            case EntityType.INT16:
                if (isArray)
                {
                    msWriter.WriteInt16Array((Int16[])val);
                }
                else
                {
                    msWriter.WriteInt16((Int16)val);
                }
                break;

            case EntityType.USHORT:
                if (isArray)
                {
                    msWriter.WriteUInt16Array((UInt16[])val);
                }
                else
                {
                    msWriter.WriteUInt16((UInt16)val);
                }
                break;

            case EntityType.INT32:
                if (isArray)
                {
                    msWriter.WriteInt32Array((Int32[])val);
                }
                else
                {
                    msWriter.WriteInt32((Int32)val);
                }
                break;

            case EntityType.CHAR:
                if (isArray)
                {
                    msWriter.WriteCharArray((char[])val);
                }
                else
                {
                    msWriter.WriteChar((char)val);
                }
                break;

            case EntityType.DECIMAL:
                if (isArray)
                {
                    msWriter.WriteDeciamlArray((decimal[])val);
                }
                else
                {
                    msWriter.WriteDecimal((decimal)val);
                }
                break;

            case EntityType.DOUBLE:
                if (isArray)
                {
                    msWriter.WriteDoubleArray((double[])val);
                }
                else
                {
                    msWriter.WriteDouble((double)val);
                }
                break;

            case EntityType.FLOAT:
                if (isArray)
                {
                    msWriter.WriteFloatArray((float[])val);
                }
                else
                {
                    msWriter.WriteFloat((float)val);
                }
                break;

            case EntityType.INT64:
                if (isArray)
                {
                    msWriter.WriteInt64Array((Int64[])val);
                }
                else
                {
                    msWriter.WriteInt64((Int64)val);
                }
                break;

            case EntityType.DATETIME:
                if (isArray)
                {
                    msWriter.WriteDateTimeArray((DateTime[])val);
                }
                else
                {
                    msWriter.WriteDateTime((DateTime)val);
                }
                break;

            case EntityType.BOOL:
                if (isArray)
                {
                    msWriter.WriteBoolArray((bool[])val);
                }
                else
                {
                    msWriter.WriteBool((bool)val);
                }
                break;

            case EntityType.ENUM:
                if (isArray)
                {
                    Array    arr    = (Array)val;
                    string[] strarr = new string[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        strarr[i] = arr.GetValue(i).ToString();
                    }
                    msWriter.WriteStringArray(strarr);
                }
                else
                {
                    msWriter.WriteString(val.ToString());
                }
                break;

            case EntityType.DICTIONARY:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var dicArray = (Array)val;
                    msWriter.WriteInt32(dicArray.Length);
                    for (int i = 0; i < dicArray.Length; i++)
                    {
                        Serialize(dicArray.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    //
                    IDictionary idic = (IDictionary)val;
                    //写入长度
                    msWriter.WriteInt32(idic.Count);
                    int i = 0;
                    foreach (var kv in idic)
                    {
                        object k = kv.Eval("Key");
                        object v = kv.Eval("Value");

                        Serialize(k, msWriter);
                        Serialize(v, msWriter);
                        i++;
                    }
                }
                break;

            case EntityType.LIST:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var listarr = (Array)val;
                    msWriter.WriteInt32(listarr.Length);
                    for (int i = 0; i < listarr.Length; i++)
                    {
                        Serialize(listarr.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var list = (IList)val;
                    msWriter.WriteInt32(list.Count);
                    foreach (var item in list)
                    {
                        Serialize(item, msWriter);
                    }
                }
                break;

            case EntityType.ARRAY:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var listarr = (Array)val;
                    msWriter.WriteInt32(listarr.Length);
                    for (int i = 0; i < listarr.Length; i++)
                    {
                        Serialize(listarr.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var arr = (Array)val;
                    msWriter.WriteInt32(arr.Length);
                    foreach (var item in arr)
                    {
                        Serialize(item, msWriter);
                    }
                }
                break;

            default:
                throw new EntityBufException("序列化错误");
            }
        }
Exemplo n.º 6
0
        private static EntityBufType MapBufType(Type type, out bool isArray)
        {
            EntityBufType ebtype = new EntityBufType();

            ebtype.ValueType = type;

            if (type.IsArray)
            //if (type.Name.EndsWith("[]"))
            {
                isArray = true;
                string typefullname = string.Format("{0}, {1}", type.FullName.Substring(0, type.FullName.LastIndexOf('[')),
                                                    type.Assembly.FullName);
                ebtype.ClassType = Type.GetType(typefullname);
            }
            else
            {
                isArray          = false;
                ebtype.ClassType = type;
            }

            string typename = ebtype.ClassType.Name;

            switch (typename)
            {
            case "Short":
                ebtype.EntityType   = EntityType.SHORT;
                ebtype.DefaultValue = default(short);
                break;

            case "UInt16":
            case "Ushort":
                ebtype.EntityType   = EntityType.USHORT;
                ebtype.DefaultValue = default(ushort);
                break;

            case "Int16":
                ebtype.EntityType   = EntityType.INT16;
                ebtype.DefaultValue = default(Int16);
                break;

            case "Int32":
                ebtype.EntityType   = EntityType.INT32;
                ebtype.DefaultValue = default(Int16);
                break;

            case "Long":
            case "Int64":
                ebtype.EntityType   = EntityType.INT64;
                ebtype.DefaultValue = default(Int64);
                break;

            case "Byte":
                ebtype.EntityType   = EntityType.BYTE;
                ebtype.DefaultValue = default(byte);
                break;

            case "Char":
                ebtype.EntityType   = EntityType.CHAR;
                ebtype.DefaultValue = default(char);
                break;

            case "Double":
                ebtype.EntityType   = EntityType.DOUBLE;
                ebtype.DefaultValue = default(double);
                break;

            case "Float":
                ebtype.EntityType   = EntityType.FLOAT;
                ebtype.DefaultValue = default(float);
                break;

            case "String":
                ebtype.EntityType   = EntityType.STRING;
                ebtype.DefaultValue = default(string);
                break;

            case "DateTime":
                ebtype.EntityType = EntityType.DATETIME;
                break;

            case "Decimal":
                ebtype.EntityType   = EntityType.DECIMAL;
                ebtype.DefaultValue = default(decimal);
                break;

            case "Boolean":
                ebtype.EntityType   = EntityType.BOOL;
                ebtype.DefaultValue = default(bool);
                break;

            case "Dictionary`2":
                ebtype.EntityType = EntityType.DICTIONARY;
                break;

            case "List`1":
                ebtype.EntityType = EntityType.LIST;
                break;

            default:
                if (isArray)
                {
                    ebtype.EntityType = EntityType.ARRAY;
                }
                else if (ebtype.ClassType.IsEnum)
                {
                    ebtype.EntityType = EntityType.ENUM;
                }
                else if (ebtype.ClassType.IsClass)
                {
                    ebtype.EntityType = EntityType.COMPLEX;
                }
                break;
            }

            return(ebtype);
        }