Example #1
0
        public static MapDef valueOf(Type type)
        {
            MapDef t = new MapDef();

            t.code = type.GetHashCode();
            t.type = type;
            MethodInfo m = type.GetMethod("get_Item");//list的索引器的返回值就是对应类型

            if (m == null)
            {
                Debuger.LogError("找不到索引器 不能确定类型:" + type.Name);
                return(null);
            }

            t.keyType   = m.GetParameters()[0].ParameterType;
            t.valueType = m.ReturnType;

            if (ProtocolCoder.CanRegister(t.keyType))
            {
                ProtocolCoder.instance.Register(t.keyType);
            }
            if (ProtocolCoder.CanRegister(t.valueType))
            {
                ProtocolCoder.instance.Register(t.valueType);
            }

            return(t);
        }
Example #2
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            // 对象解析
            value = Activator.CreateInstance(type);
            IDictionary dict = (IDictionary)value;

            if (dict == null)
            {
                Debuger.LogError("类型不是map:{0}", type.Name);

                return(false);
            }

            //获取key/value的类型
            MapDef def = ProtocolCoder.instance.getMapDef(type);

            if (def == null)
            {
                Debuger.LogError("map定义{0}不存在", type.FullName);
                return(false);
            }
            Type keyType   = def.keyType;
            Type valueType = def.valueType;

            //取键数
            int len;

            if (!readVarInt32(ioBuffer, out len))
            {
                return(false);
            }

            BeginParentLog("Dictionary", len);
            for (int i = 0; i < len; i++)
            {
                AddLogNewLine("[key]:");//加个分隔符
                object keyObj;
                if (!ProtocolCoder.instance.Decode(ioBuffer, keyType, out keyObj))
                {
                    return(false);
                }
                AddLog(" [value]:");//加个分隔符
                object valueObj;
                if (!ProtocolCoder.instance.Decode(ioBuffer, valueType, out valueObj))
                {
                    return(false);
                }
                dict.Add(keyObj, valueObj);
                AddLog(",");//加个分隔符
            }
            EndParentLog();

            return(true);
        }
Example #3
0
        public void Register(System.Type clz)
        {
            if (clz.IsEnum)
            {
                if (m_enumDefs.Get(clz) == null)
                {
                    EnumDef def = EnumDef.valueOf(clz);
                    if (def == null)
                    {
                        return;
                    }
                    m_enumIdxs.Add(def.code, def);
                    m_enumDefs.Add(clz, def);
                    m_log.AppendFormat("Protocol注册了枚举:{0} code:{1} \n", clz.Name, def.code);
                }
            }
            else if (!clz.IsClass)
            {
                Debuger.LogError("不能注册的类型:{0}", clz.Name);
            }
            else if (clz.IsArray)
            {
                System.Type elemType = clz.GetElementType();
                if (ProtocolCoder.CanRegister(elemType))
                {
                    ProtocolCoder.instance.Register(elemType);
                }
            }
            else if (clz.GetInterface("System.Collections.IList") != null)
            {
                if (m_collectionDefs.Get(clz) == null)
                {
                    CollectionDef def = CollectionDef.valueOf(clz);
                    if (def == null)
                    {
                        return;
                    }
                    m_collectionIdxs.Add(def.code, def);
                    m_collectionDefs.Add(clz, def);
                    m_log.AppendFormat("Protocol注册了list:{0} code:{1} element:{2} \n", clz.Name, def.code, def.elementType.Name);
                }
            }
            else if (clz.GetInterface("System.Collections.IDictionary") != null)
            {
                if (m_mapDefs.Get(clz) == null)
                {
                    MapDef def = MapDef.valueOf(clz);
                    if (def == null)
                    {
                        return;
                    }
                    m_mapIdxs.Add(def.code, def);
                    m_mapDefs.Add(clz, def);
                    m_log.AppendFormat("Protocol注册了map:{0} code:{1} key:{2} value:{3} \n", clz.Name, def.code, def.keyType.Name, def.valueType.Name);
                }
            }
            else
            {
                if (m_typeDefs.Get(clz) == null)
                {
                    if (!IsObjectType(clz))
                    {
                        Debuger.LogError("不能序列化的类型,不能序列化模板类型、c#原生类型、继承自其他类的类型:{0}", clz.Name);
                        return;
                    }
                    TimeCheck check = new TimeCheck();
                    TypeDef   def   = TypeDef.valueOf(clz, m_typeDefs);
                    if (def == null)
                    {
                        return;
                    }
                    m_typeIdxs.Add(def.code, def);

                    m_log.AppendFormat("Protocol注册了类:{0} code:{1} 耗时:{2} \n", clz.Name, def.code, check.delayMS);
                }
            }
        }