Ejemplo n.º 1
0
        static object DeserializationBitConverter(byte[] ion, Type type, ref int index)
        {
            string typeName = type.Name;

            if (Common.CustomIsPrimitive(type))
            {
                switch (typeName)
                {
                case "String":
                {
                    int length = ion.Length > 0 ? BitConverter.ToInt32(ion, index) : 0;
                    index += 4;
                    string obj = length > 0 ? ion.ByteToString(index, length): null;
                    index += length;
                    return(obj);
                }

                case "Boolean":
                {
                    return(BoolManager.NextBool(ion[index], ref index));
                }

                case "Byte":
                {
                    byte obj = ion[index];
                    index += 1;
                    return(obj);
                }

                case "Int16":
                {
                    short obj = BitConverter.ToInt16(ion, index);
                    index += 2;
                    return(obj);
                }

                case "UInt16":
                {
                    ushort obj = BitConverter.ToUInt16(ion, index);
                    index += 2;
                    return(obj);
                }

                case "Int32":
                {
                    int obj = BitConverter.ToInt32(ion, index);
                    index += 4;
                    return(obj);
                }

                case "UInt32":
                {
                    uint obj = BitConverter.ToUInt32(ion, index);
                    index += 4;
                    return(obj);
                }

                case "Int64":
                {
                    long obj = BitConverter.ToInt64(ion, index);
                    index += 8;
                    return(obj);
                }

                case "UInt64":
                {
                    ulong obj = BitConverter.ToUInt64(ion, index);
                    index += 8;
                    return(obj);
                }

                case "Single":
                {
                    float obj = BitConverter.ToSingle(ion, index);
                    index += 4;
                    return(obj);
                }

                case "Double":
                {
                    double obj = BitConverter.ToDouble(ion, index);
                    index += 8;
                    return(obj);
                }

                case "Decimal":
                {
                    decimal obj = BitConverterExt.ToDecimal(ion, index);
                    index += 16;
                    return(obj);
                }

                default:
                    throw new Exception("ION doesn't know how to deserialize type " + typeName);
                }
            }
            else if (type.IsEnum)
            {
                object obj = Enum.Parse(type, ion[index].ToString());
                index += 1;
                return(obj);
            }
            else
            {
                byte nextByte    = index < ion.Length ? ion[index] : (byte)0;
                bool activeclass = BoolManager.NextBool(nextByte, ref index);
                if (activeclass)
                {
                    MethodInfo method  = typeof(ION).GetMethod("FromION", staticMethod);
                    MethodInfo generic = method.MakeGenericMethod(type);
                    object[]   args    = new object[] { ion, index };
                    object     obj     = generic.Invoke(null, args);
                    index = (int)args[1];
                    return(obj);
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 2
0
        static byte[] SerializationBitConverter(Type Type, object Object, string TypeName, ref int index)
        {
            if (Common.CustomIsPrimitive(Type))
            {
                switch (TypeName)
                {
                case "String":
                    List <byte> ion = new List <byte>();
                    if (Object != null)
                    {
                        string str = Object.ToString();
                        ion.AddRange(BitConverter.GetBytes(str.Length));
                        index += 4;
                        ion.AddRange(str.StringToByte());
                        index += str.Length;
                    }
                    else
                    {
                        ion.AddRange(BitConverter.GetBytes(0));
                        index += 4;
                    }
                    return(ion.ToArray());

                case "Boolean":
                    bool           b   = Convert.ToBoolean(Object);
                    BoolEditResult res = BoolManager.AddBool(b, ref index);
                    return(new byte[0]);

                case "Byte":
                    index += 1;
                    return(new byte[1] {
                        (byte)Object
                    });

                case "Int16":
                    index += 2;
                    return(BitConverter.GetBytes(Convert.ToInt16(Object)));

                case "UInt16":
                    index += 2;
                    return(BitConverter.GetBytes(Convert.ToUInt16(Object)));

                case "Int32":
                    index += 4;
                    return(BitConverter.GetBytes(Convert.ToInt32(Object)));

                case "UInt32":
                    index += 4;
                    return(BitConverter.GetBytes(Convert.ToUInt32(Object)));

                case "Int64":
                    index += 8;
                    return(BitConverter.GetBytes(Convert.ToInt64(Object)));

                case "UInt64":
                    index += 8;
                    return(BitConverter.GetBytes(Convert.ToUInt64(Object)));

                case "Single":
                    index += 4;
                    return(BitConverter.GetBytes(float.Parse(Object.ToString())));

                case "Double":
                    index += 8;
                    return(BitConverter.GetBytes(Convert.ToDouble(Object)));

                case "Decimal":
                    index += 16;
                    return(BitConverterExt.GetBytes(Convert.ToDecimal(Object)));

                default:
                    throw new Exception("ION doesn't know how to serialize type " + TypeName);
                }
            }
            else if (Type.IsEnum)
            {
                byte enumByte = (byte)Convert.ChangeType(Object, typeof(byte));
                index += 1;
                return(new byte[1] {
                    enumByte
                });
            }
            else
            {
                List <byte>    ion = new List <byte>();
                BoolEditResult res = new BoolEditResult();
                if (Object != null)
                {
                    res = BoolManager.AddBool(true, ref index);
                    ion.AddRange(ToION(Object, ref index));
                }
                else
                {
                    res = BoolManager.AddBool(false, ref index);
                }

                return(ion.ToArray());
            }
        }