Beispiel #1
0
    //Read functions search
    private new static Func <SimpleByteWriter, object> GetReaderFunction(Type type)
    {
        Func <SimpleByteWriter, object> result = null;

        if (readFunctions.TryGetValue(type, out result))
        {
            //is one of the primitive types
            return(result);
        }
        else
        {
            //isn't a primitive type, check if it's a collection or dictionary

            //Check cached lists first to avoid reflection on previously encountered types
            if (arrayTypes.Contains(type))
            {
                Type itemType = IListToItemType[type];
                Func <SimpleByteWriter, object> arrayReadFunction = (SimpleByteWriter writer) => { return(writer.ReadArray(itemType, GetReaderFunction)); };
                return(arrayReadFunction);
            }
            else if (dictionaryTypes.Contains(type))
            {
                Func <SimpleByteWriter, object> dictionaryReadFunction = (SimpleByteWriter writer) => { return(writer.ReadDictionary(type, GetReaderFunction)); };
                return(dictionaryReadFunction);
            }
            else if (IListTypes.Contains(type))
            {
                Func <SimpleByteWriter, object> listReadFunction = (SimpleByteWriter writer) => { return(writer.ReadList(type, GetReaderFunction)); };
                return(listReadFunction);
            }
            else if (unsupportedTypes.Contains(type))
            {
                return(null);
            }

            //not in cach yet, try to determine if we have a valid write function, add to cache if we find a valid function
            if (type.IsArray)
            {
                arrayTypes.Add(type);
                Type itemType = type.GetElementType();
                IListToItemType[type] = itemType;
                Func <SimpleByteWriter, object> arrayReadFunction = (SimpleByteWriter writer) => { return(writer.ReadArray(itemType, GetReaderFunction)); };
                return(arrayReadFunction);
            }
            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                dictionaryTypes.Add(type);
                Type[] genericArguments = type.GetGenericArguments();
                IDictionaryToItemTypes[type] = new KeyValuePair <Type, Type>(genericArguments[0], genericArguments[1]);

                Func <SimpleByteWriter, object> dictionaryReadFunction = (SimpleByteWriter writer) => { return(writer.ReadDictionary(type, GetReaderFunction)); };
                return(dictionaryReadFunction);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                IListTypes.Add(type);
                Type itemType = type.GetGenericArguments()[0];
                IListToItemType[type] = itemType;
                Func <SimpleByteWriter, object> listReadFunction = (SimpleByteWriter writer) => { return(writer.ReadList(type, GetReaderFunction)); };
                return(listReadFunction);
            }
            else
            {
                //Is compound object
                compoundTypes.Add(type);
                Func <SimpleByteWriter, object> compundReadFunction = (SimpleByteWriter writer) => { return(((RobustSerializer)writer).readCompoundObject(type)); };
                return(compundReadFunction);
            }
        }
    }
Beispiel #2
0
    //Write functions search
    private new static Action <SimpleByteWriter, object> GetWriterFunction(Type type)
    {
        //Debug.Log("ByteWriter GetWriterFunction() type:" + type.ToString());

        Action <SimpleByteWriter, object> result = null;

        if (writeFunctions.TryGetValue(type, out result))
        {
            //is one of the primitive types
            return(result);
        }
        else
        {
            //isn't a primitive type, check if it's a collection or dictionary

            //Check cached lists first to avoid reflection on previously encountered types
            if (arrayTypes.Contains(type))
            {
                return(ArrayWriteFunction);
            }
            else if (dictionaryTypes.Contains(type))
            {
                return(IDictionaryWriteFunction);
            }
            else if (IListTypes.Contains(type))
            {
                return(IListWriteFunction);
            }
            else if (unsupportedTypes.Contains(type))
            {
                return(null);
            }

            //not in cach yet, try to determine if we have a valid write function, add to cache if we find a valid function
            if (type.IsArray)
            {
                arrayTypes.Add(type);
                IListToItemType[type] = type.GetElementType();
                return(ArrayWriteFunction);
            }
            else if (typeof(IDictionary).IsAssignableFrom(type))
            {
                dictionaryTypes.Add(type);
                Type[] genericArguments = type.GetGenericArguments();
                IDictionaryToItemTypes[type] = new KeyValuePair <Type, Type>(genericArguments[0], genericArguments[1]);
                return(IDictionaryWriteFunction);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                IListTypes.Add(type);
                IListToItemType[type] = type.GetGenericArguments()[0];
                return(IListWriteFunction);
            }
            else
            {
                //Is compound object
                compoundTypes.Add(type);
                Action <SimpleByteWriter, object> compundWriteFunction = (SimpleByteWriter writer, object o) => { ((RobustSerializer)writer).writeCompoundObject(o, type); };
                return(compundWriteFunction);
            }
        }
    }