Beispiel #1
0
        internal static void WriteRootBasicType(BinaryWriter writer, object obj, Type type, BoisBasicTypeInfo typeInfo, Encoding encoding)
        {
            switch (typeInfo.KnownType)
            {
            case EnBasicKnownType.String:
                PrimitiveWriter.WriteValue(writer, obj as string, encoding);
                return;

            case EnBasicKnownType.Char:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as char?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (char)obj);
                }
                return;

            case EnBasicKnownType.Guid:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as Guid?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (Guid)obj);
                }
                return;

            case EnBasicKnownType.Bool:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as bool?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (bool)obj);
                }
                return;

            case EnBasicKnownType.Enum:
                if (obj == null)
                {
                    PrimitiveWriter.WriteNullValue(writer);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, obj as Enum, type);
                }
                return;

            case EnBasicKnownType.DateTime:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as DateTime?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (DateTime)obj);
                }
                return;

            case EnBasicKnownType.DateTimeOffset:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as DateTimeOffset?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (DateTimeOffset)obj);
                }
                return;

            case EnBasicKnownType.TimeSpan:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as TimeSpan?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (TimeSpan)obj);
                }
                return;

            case EnBasicKnownType.ByteArray:
                PrimitiveWriter.WriteValue(writer, obj as byte[]);
                return;

            case EnBasicKnownType.KnownTypeArray:

                // calling for subitem
                WriteRootBasicTypedArray(writer, obj as Array, typeInfo, encoding);
                return;

            case EnBasicKnownType.Color:
                if (typeInfo.IsNullable)
                {
                    PrimitiveWriter.WriteValue(writer, obj as Color?);
                }
                else
                {
                    PrimitiveWriter.WriteValue(writer, (Color)obj);
                }
                break;

            case EnBasicKnownType.Version:
                PrimitiveWriter.WriteValue(writer, obj as Version);
                return;

            case EnBasicKnownType.DbNull:
                PrimitiveWriter.WriteValue(writer, obj as DBNull);
                return;

            case EnBasicKnownType.Uri:
                PrimitiveWriter.WriteValue(writer, (obj as Uri));
                break;

            case EnBasicKnownType.DataTable:
                PrimitiveWriter.WriteValue(writer, obj as DataTable, encoding);
                return;

            case EnBasicKnownType.DataSet:
                PrimitiveWriter.WriteValue(writer, obj as DataSet, encoding);
                return;

            case EnBasicKnownType.Int16:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as short?);
                }
                else
                {
                    NumericSerializers.WriteVarInt(writer, (short)obj);
                }
                break;

            case EnBasicKnownType.Int32:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as int?);
                }
                else
                {
                    NumericSerializers.WriteVarInt(writer, (int)obj);
                }
                return;

            case EnBasicKnownType.Int64:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as long?);
                }
                else
                {
                    NumericSerializers.WriteVarInt(writer, (long)obj);
                }
                return;

            case EnBasicKnownType.UInt16:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as ushort?);
                }
                else
                {
                    NumericSerializers.WriteVarInt(writer, (ushort)obj);
                }
                return;

            case EnBasicKnownType.UInt32:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as uint?);
                }
                else
                {
                    NumericSerializers.WriteVarInt(writer, (uint)obj);
                }
                return;

            case EnBasicKnownType.UInt64:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as ulong?);
                }
                else
                {
                    NumericSerializers.WriteVarInt(writer, (ulong)obj);
                }
                return;

            case EnBasicKnownType.Double:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarDecimal(writer, obj as double?);
                }
                else
                {
                    NumericSerializers.WriteVarDecimal(writer, (double)obj);
                }
                return;

            case EnBasicKnownType.Decimal:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarDecimal(writer, obj as decimal?);
                }
                else
                {
                    NumericSerializers.WriteVarDecimal(writer, (decimal)obj);
                }
                return;

            case EnBasicKnownType.Single:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarDecimal(writer, obj as float?);
                }
                else
                {
                    NumericSerializers.WriteVarDecimal(writer, (float)obj);
                }
                return;

            case EnBasicKnownType.Byte:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as byte?);
                }
                else
                {
                    writer.Write((byte)obj);
                }
                return;

            case EnBasicKnownType.SByte:
                if (typeInfo.IsNullable)
                {
                    NumericSerializers.WriteVarInt(writer, obj as sbyte?);
                }
                else
                {
                    writer.Write((sbyte)obj);
                }
                return;


            case EnBasicKnownType.Unknown:
            default:
                // should never reach here
                throw new Exception($"Not supported type '{type}' as root");
            }
        }
Beispiel #2
0
 internal static void WriteValue(BinaryWriter writer, Uri uri)
 {
     PrimitiveWriter.WriteValue(writer, uri?.ToString(), Encoding.UTF8);
 }