Esempio n. 1
0
        /*
         *  Allows us to call the generic Read method using Reflection so we can define the generic parameter at runtime.
         *  It also caches the method to improve performance in later calls.
         */
        public object Read(ES3Reader reader)
        {
            if (reader.StartReadDictionary())
            {
                return(null);
            }

            var dict = (IDictionary)ES3Reflection.CreateInstance(type);

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadDictionaryKey())
                {
                    return(dict);
                }
                var key = reader.Read <object>(keyType);
                reader.EndReadDictionaryKey();

                reader.StartReadDictionaryValue();
                var value = reader.Read <object>(valueType);

                dict.Add(key, value);

                if (reader.EndReadDictionaryValue())
                {
                    break;
                }
            }

            reader.EndReadDictionary();

            return(dict);
        }
Esempio n. 2
0
        public Dictionary <TKey, TVal> ReadKVP <TKey, TVal>(ES3Reader reader)
        {
            if (reader.StartReadDictionary())
            {
                return(null);
            }

            var dict = new Dictionary <TKey, TVal>();

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadDictionaryKey())
                {
                    return(dict);
                }
                TKey key = reader.Read <TKey>(keyType);
                reader.EndReadDictionaryKey();

                reader.StartReadDictionaryValue();
                TVal value = reader.Read <TVal>(valueType);

                dict.Add(key, value);

                if (reader.EndReadDictionaryValue())
                {
                    break;
                }
            }

            reader.EndReadDictionary();

            return(dict);
        }
Esempio n. 3
0
        public void ReadInto(ES3Reader reader, object obj)
        {
            if (reader.StartReadDictionary())
            {
                throw new NullReferenceException("The Dictionary we are trying to load is stored as null, which is not allowed when using ReadInto methods.");
            }

            var dict = (IDictionary)obj;

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadDictionaryKey())
                {
                    return;
                }
                var key = reader.Read <object>(keyType);

                if (!dict.Contains(key))
                {
                    throw new KeyNotFoundException("The key \"" + key + "\" in the Dictionary we are loading does not exist in the Dictionary we are loading into");
                }
                var value = dict[key];
                reader.EndReadDictionaryKey();

                reader.StartReadDictionaryValue();

                reader.ReadInto <object>(value, valueType);

                if (reader.EndReadDictionaryValue())
                {
                    break;
                }
            }

            reader.EndReadDictionary();
        }