Пример #1
0
        public override void WriteValueNotExisted(HessianWriter writer, HessianContext context, object value)
        {
            Type type = value.GetType();

            if (!type.IsArray)
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            var elementType = type.GetElementType();

            if (elementType == typeof(byte) || elementType == typeof(sbyte))
            {
                GetConverter <BinaryConverter>().WriteValueNotNull(writer, context, value);
                return;
            }

            var array = (Array)value;
            HessianConverter itemConverter = null;

            if (elementType == typeof(object))
            {
                //untyped array
                if (array.Length <= Constants.LIST_DIRECT_MAX)
                {
                    writer.Write((byte)(Constants.BC_LIST_DIRECT_UNTYPED + array.Length));
                }
                else
                {
                    writer.Write(Constants.BC_LIST_FIXED_UNTYPED);
                    IntConverter.WriteInt(writer, context, array.Length);
                }
                itemConverter = AutoConverter;
            }
            else
            {
                if (array.Length <= Constants.LIST_DIRECT_MAX)
                {
                    writer.Write((byte)(Constants.BC_LIST_DIRECT + array.Length));
                    TypeConverter.WriteType(writer, context, type);
                }
                else
                {
                    writer.Write(Constants.BC_LIST_FIXED);
                    TypeConverter.WriteType(writer, context, type);
                    IntConverter.WriteInt(writer, context, array.Length);
                }
                itemConverter = AutoConverter.GetConverter(elementType);
            }

            foreach (var item in array)
            {
                itemConverter.WriteValue(writer, context, item);
            }
        }
Пример #2
0
        public static T GetConverter <T>() where T : HessianConverter
        {
            HessianConverter converter = null;
            var type = typeof(T);

            if (!ConverterCache.TryGetValue(type, out converter))
            {
                converter = Activator.CreateInstance <T>();
                ConverterCache.Add(type, converter);
            }
            return((T)converter);
        }
Пример #3
0
        public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
        {
            int  len         = 0;
            Type elementType = null;

            if (0x70 <= initialOctet && initialOctet <= 0x77)
            {
                elementType = ((Type)TypeConverter.ReadValue(reader, context, null)).GetElementType();
                len         = initialOctet - 0x70;
            }
            else if (0x78 <= initialOctet && initialOctet <= 0x7f)
            {
                elementType = typeof(object);
                len         = initialOctet - 0x78;
            }
            else if (Constants.BC_LIST_FIXED_UNTYPED == initialOctet)
            {
                elementType = typeof(object);
                len         = (int)IntConverter.ReadValue(reader, context, typeof(int));
            }
            else if (Constants.BC_LIST_FIXED == initialOctet)
            {
                elementType = ((Type)TypeConverter.ReadValue(reader, context, null)).GetElementType();
                len         = (int)IntConverter.ReadValue(reader, context, typeof(int));
            }

            var array = Array.CreateInstance(elementType, len);

            context.ValueRefs.AddItem(array);

            HessianConverter itemConverter = null;

            if (elementType == typeof(object))
            {
                itemConverter = AutoConverter;
            }
            else
            {
                itemConverter = AutoConverter.GetConverter(elementType);
            }

            for (int i = 0; i < len; i++)
            {
                var item = itemConverter.ReadValue(reader, context, elementType);
                array.SetValue(item, i);
            }
            return(array);
        }
Пример #4
0
        public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
        {
            Type             listType      = null;
            Type             elementType   = null;
            HessianConverter itemConverter = null;

            if (Constants.BC_LIST_VARIABLE_UNTYPED == initialOctet)
            {
                listType      = typeof(List <object>);
                elementType   = typeof(object);
                itemConverter = AutoConverter;
            }
            else if (Constants.BC_LIST_VARIABLE == initialOctet)
            {
                listType      = (Type)TypeConverter.ReadValue(reader, context, null);
                elementType   = listType.GenericTypeArguments[0];
                itemConverter = AutoConverter.GetConverter(elementType);
            }
            else
            {
                throw Exceptions.UnExpectedInitialOctet(this, initialOctet);
            }

            var addMethod = listType.GetMethod("Add");
            var list      = Activator.CreateInstance(listType);

            context.ValueRefs.AddItem(list);

            initialOctet = reader.ReadByte();
            var parameters = new object[1];

            while (Constants.BC_END != initialOctet)
            {
                parameters[0] = itemConverter.ReadValue(reader, context, elementType, initialOctet);
                addMethod.Invoke(list, parameters);
                initialOctet = reader.ReadByte();
            }
            return(list);
        }
Пример #5
0
        public override void WriteValueNotExisted(HessianWriter writer, HessianContext context, object value)
        {
            Type type     = value.GetType();
            Type itemType = null;

            if (type == typeof(ArrayList))
            {
                itemType = typeof(object);
            }
            else if (IsGenericList(type))
            {
                itemType = type.GenericTypeArguments[0];
            }
            else
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            HessianConverter itemConverter = null;

            if (itemType == typeof(object))
            {
                writer.Write(Constants.BC_LIST_VARIABLE_UNTYPED);
                itemConverter = AutoConverter;
            }
            else
            {
                writer.Write(Constants.BC_LIST_VARIABLE);
                TypeConverter.WriteType(writer, context, type);
                itemConverter = AutoConverter.GetConverter(itemType);
            }

            foreach (var item in (IEnumerable)value)
            {
                itemConverter.WriteValue(writer, context, item);
            }
            writer.Write(Constants.BC_END);
        }