예제 #1
0
        public Dictionary <TKey, TValue> ReadDictionary <TKey, TValue> ()
        {
            uint ln = ReadUInt32();

            if (ln > ProtocolInformation.MaxArrayLength)
            {
                throw new ProtocolException("Dict length " + ln + " exceeds maximum allowed " + ProtocolInformation.MaxArrayLength + " bytes");
            }

            var val = new Dictionary <TKey, TValue> ((int)(ln / 8));

            ReadPad(8);

            int endPos = _pos + (int)ln;

            var keyReader   = ReadMethodFactory.CreateReadMethodDelegate <TKey>();
            var valueReader = ReadMethodFactory.CreateReadMethodDelegate <TValue>();

            while (_pos < endPos)
            {
                ReadPad(8);
                TKey   k = keyReader(this);
                TValue v = valueReader(this);
                val.Add(k, v);
            }

            if (_pos != endPos)
            {
                throw new ProtocolException("Read pos " + _pos + " != ep " + endPos);
            }

            return(val);
        }
예제 #2
0
 public void LittleEndian(
     Type type,
     object expectedValue,
     int alignment,
     byte[] bigEndianData,
     byte[] littleEndianData,
     IEqualityComparer <object> returnComparer)
 {
     // via Delegate
     {
         MessageReader reader             = CreateMessageReader(EndianFlag.Little, littleEndianData);
         var           method             = s_createReadDelegateMethod.MakeGenericMethod(type);
         var           readMethodDelegate = (Delegate)method.Invoke(null, null);
         var           readValue          = readMethodDelegate.DynamicInvoke(new object[] { reader });
         Assert.IsAssignableFrom(type, readValue);
         if (returnComparer != null)
         {
             Assert.Equal(expectedValue, readValue, returnComparer);
         }
         else
         {
             Assert.Equal(expectedValue, readValue);
         }
     }
     // via ReadMethod
     {
         if (type.GetTypeInfo().IsEnum)
         {
             // TODO
             return;
         }
         MessageReader reader         = CreateMessageReader(EndianFlag.Little, littleEndianData);
         var           ReadMethodInfo = ReadMethodFactory.CreateReadMethodForType(type);
         object        readValue;
         if (ReadMethodInfo.IsStatic)
         {
             readValue = ReadMethodInfo.Invoke(null, new object[] { reader });
         }
         else
         {
             readValue = ReadMethodInfo.Invoke(reader, null);
         }
         Assert.IsAssignableFrom(type, readValue);
         if (returnComparer != null)
         {
             Assert.Equal(expectedValue, readValue, returnComparer);
         }
         else
         {
             Assert.Equal(expectedValue, readValue);
         }
     }
 }
예제 #3
0
        public void Invalid(Type type)
        {
            var       method    = s_createReadDelegateMethod.MakeGenericMethod(type);
            Exception exception = null;

            try
            {
                method.Invoke(null, null);
            }
            catch (TargetInvocationException tie)
            {
                exception = tie.InnerException;
            }
            Assert.IsAssignableFrom <ArgumentException>(exception);
            Assert.Throws <ArgumentException>(() => ReadMethodFactory.CreateReadMethodForType(type));
        }
예제 #4
0
        public T[] ReadArray <T> ()
        {
            uint ln       = ReadUInt32();
            Type elemType = typeof(T);

            if (ln > ProtocolInformation.MaxArrayLength)
            {
                throw new ProtocolException("Array length " + ln + " exceeds maximum allowed " + ProtocolInformation.MaxArrayLength + " bytes");
            }

            //advance to the alignment of the element
            ReadPad(ProtocolInformation.GetAlignment(elemType));

            if (elemType.GetTypeInfo().IsPrimitive)
            {
                // Fast path for primitive types (except bool which isn't blittable and take another path)
                if (elemType != typeof(bool))
                {
                    return(MarshalArray <T> (ln));
                }
                else
                {
                    return((T[])(Array)MarshalBoolArray(ln));
                }
            }

            var list   = new List <T> ();
            int endPos = _pos + (int)ln;

            var elementReader = ReadMethodFactory.CreateReadMethodDelegate <T>();

            while (_pos < endPos)
            {
                list.Add(elementReader(this));
            }

            if (_pos != endPos)
            {
                throw new ProtocolException("Read pos " + _pos + " != ep " + endPos);
            }

            return(list.ToArray());
        }
예제 #5
0
        public object Read(Type type)
        {
            if (type.GetTypeInfo().IsEnum)
            {
                var value = Read(Enum.GetUnderlyingType(type));
                return(Enum.ToObject(type, value));
            }

            if (type == typeof(bool))
            {
                return(ReadBoolean());
            }
            else if (type == typeof(byte))
            {
                return(ReadByte());
            }
            else if (type == typeof(double))
            {
                return(ReadDouble());
            }
            else if (type == typeof(short))
            {
                return(ReadInt16());
            }
            else if (type == typeof(int))
            {
                return(ReadInt32());
            }
            else if (type == typeof(long))
            {
                return(ReadInt64());
            }
            else if (type == typeof(ObjectPath))
            {
                return(ReadObjectPath());
            }
            else if (type == typeof(Signature))
            {
                return(ReadSignature());
            }
            else if (type == typeof(string))
            {
                return(ReadString());
            }
            else if (type == typeof(float))
            {
                return(ReadSingle());
            }
            else if (type == typeof(ushort))
            {
                return(ReadUInt16());
            }
            else if (type == typeof(uint))
            {
                return(ReadUInt32());
            }
            else if (type == typeof(ulong))
            {
                return(ReadUInt64());
            }
            else if (type == typeof(object))
            {
                return(ReadVariant());
            }
            else if (type == typeof(IDBusObject))
            {
                return(ReadBusObject());
            }

            var method = ReadMethodFactory.CreateReadMethodForType(type);

            if (method.IsStatic)
            {
                return(method.Invoke(null, new object[] { this }));
            }
            else
            {
                return(method.Invoke(this, null));
            }
        }