Пример #1
0
        void WriteValue(BinaryWriter writer, BoisMemberInfo boisMemInfo, object value)
        {
            if (!boisMemInfo.IsSupportedPrimitive && !boisMemInfo.IsContainerObject)
            {
                if (value == null)
                {
                    WriteNullableType(writer, true);
                    return;
                }
                else if (boisMemInfo.IsNullable)
                {
                    WriteNullableType(writer, false);
                }
            }

            switch (boisMemInfo.KnownType)
            {
            case EnBoisKnownType.Unknown:

                if (boisMemInfo.IsContainerObject)
                {
                    WriteObject(writer, boisMemInfo, value);
                }
                else if (boisMemInfo.IsStringDictionary)
                {
                    WriteStringDictionary(writer, value as IDictionary);
                }
                else if (boisMemInfo.IsDictionary)
                {
                    WriteDictionary(writer, value as IDictionary);
                }
                else if (boisMemInfo.IsCollection || boisMemInfo.IsArray)
                {
                    if (boisMemInfo.IsGeneric)
                    {
                        WriteGenericList(writer, value as IEnumerable);
                    }
                    else
                    {
                        WriteArray(writer, value);
                    }
                }
                break;

            case EnBoisKnownType.Int16:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarInt(writer, (short?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarInt(writer, (short)value);
                }
                break;

            case EnBoisKnownType.UInt16:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarInt(writer, (ushort?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarInt(writer, (ushort)value);
                }
                break;

            case EnBoisKnownType.Int32:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarInt(writer, (int?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarInt(writer, (int)value);
                }

                break;

            case EnBoisKnownType.Int64:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarInt(writer, (long?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarInt(writer, (long)value);
                }
                break;

            case EnBoisKnownType.UInt64:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarInt(writer, (ulong?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarInt(writer, (ulong)value);
                }
                break;

            case EnBoisKnownType.UInt32:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarInt(writer, (uint?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarInt(writer, (uint)value);
                }
                break;

            case EnBoisKnownType.Double:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarDecimal(writer, (double?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarDecimal(writer, (double)value);
                }
                break;

            case EnBoisKnownType.Decimal:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarDecimal(writer, (decimal?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarDecimal(writer, (decimal)value);
                }
                break;

            case EnBoisKnownType.Single:
                if (value == null || boisMemInfo.IsNullable)
                {
                    PrimitivesConvertion.WriteVarDecimal(writer, (float?)value);
                }
                else
                {
                    PrimitivesConvertion.WriteVarDecimal(writer, (float)value);
                }
                break;

            case EnBoisKnownType.Byte:
                writer.Write((byte)value);
                break;

            case EnBoisKnownType.SByte:
                writer.Write((sbyte)value);
                break;

            case EnBoisKnownType.ByteArray:
                WriteBytes(writer, (byte[])value);
                break;

            case EnBoisKnownType.String:
                WriteString(writer, value as string);
                break;

            case EnBoisKnownType.Char:
                writer.Write((ushort)((char)value));
                break;

            case EnBoisKnownType.Bool:
                writer.Write((byte)(((bool)value) ? 1 : 0));
                break;

            case EnBoisKnownType.Enum:
                WriteEnum(writer, (Enum)value);
                break;

            case EnBoisKnownType.DateTime:
                WriteDateTime(writer, (DateTime)value);
                break;

            case EnBoisKnownType.DateTimeOffset:
                WriteDateTimeOffset(writer, (DateTimeOffset)value);
                break;

            case EnBoisKnownType.TimeSpan:
                WriteTimeSpan(writer, (TimeSpan)value);
                break;

#if !SILVERLIGHT && DotNet
            case EnBoisKnownType.DataSet:
                WriteDataset(writer, value as DataSet);
                break;

            case EnBoisKnownType.DataTable:
                WriteDataTable(writer, value as DataTable);
                break;
#endif
#if DotNet || DotNetCore || DotNetStandard
            case EnBoisKnownType.NameValueColl:
                WriteCollectionNameValue(writer, value as NameValueCollection);
                break;

            case EnBoisKnownType.Color:
                WriteColor(writer, (Color)value);
                break;
#endif


            case EnBoisKnownType.Version:
                WriteVersion(writer, value as Version);
                break;

            case EnBoisKnownType.DbNull:
                // Do not write anything, it is already written as Nullable object. //WriteNullableType(true);
                break;

            case EnBoisKnownType.Guid:
                WriteGuid(writer, (Guid)value);
                break;

            case EnBoisKnownType.Uri:
                WriteUri(writer, value as Uri);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }