コード例 #1
0
        private static void WriteGenericTypeArgument(BinaryWriter writer, Type type)
        {
            var objTypeCode = SerializerUtil.GetObjectTypeCode(type, null);

            writer.Write((byte)objTypeCode);

            switch (objTypeCode)
            {
            case ObjectTypeCode.Serializable:
                writer.Write(type.AssemblyQualifiedName);
                break;

            case ObjectTypeCode.Primitive: {
                var primitiveCode = SerializerUtil.GetPrimitiveCode(type);
                writer.Write(SerializerUtil.IsNullable(type));
                writer.Write((byte)primitiveCode);
                break;
            }

            case ObjectTypeCode.Object:
                break;

            default:
                throw new SerializationException($"The type {type} is not a supported generic agument type");
            }
        }
コード例 #2
0
        private static void WriteArray(BinaryWriter writer, Type objectType, object obj)
        {
            var elementType  = objectType.GetElementType();
            var elemTypeCode = SerializerUtil.GetObjectTypeCode(elementType, obj);

            writer.Write((byte)elemTypeCode);

            switch (elemTypeCode)
            {
            case ObjectTypeCode.Primitive:
                var primitiveTypeCode = SerializerUtil.GetPrimitiveCode(elementType);
                writer.Write((byte)primitiveTypeCode);
                break;

            case ObjectTypeCode.Serializable:
                var name = elementType.AssemblyQualifiedName;

                writer.Write(name);
                break;

            case ObjectTypeCode.Object:
                break;

            default:
                throw new NotSupportedException($"Array of type {elementType} is not supported");
            }

            if (obj == null)
            {
                return;
            }

            var array = (Array)obj;

            if (array.Rank > 1)
            {
                throw new NotSupportedException("Multi-dimensional arrays not supported");
            }

            writer.Write(array.Length);

            for (int i = 0; i < array.Length; i++)
            {
                var value = array.GetValue(i);
                WriteObject(writer, elementType, value);
            }
        }
コード例 #3
0
        private static void WriteObject(BinaryWriter writer, Type objectType, object obj)
        {
            var objTypeCode = SerializerUtil.GetObjectTypeCode(objectType, obj);

            writer.Write((byte)objTypeCode);

            writer.Write(obj == null ? (byte)0 : (byte)1);

            switch (objTypeCode)
            {
            case ObjectTypeCode.Serializable:
                WriteSerializable(writer, objectType, obj);
                break;

            case ObjectTypeCode.Object:
                WriteUnknownObject(writer, obj);
                break;

            case ObjectTypeCode.Primitive:
                WritePrimitive(writer, objectType, obj);
                break;

            case ObjectTypeCode.Enum:
                WriteEnum(writer, objectType, obj);
                break;

            case ObjectTypeCode.Array:
                WriteArray(writer, objectType, obj);
                break;

            case ObjectTypeCode.List:
                WriteList(writer, objectType, obj);
                break;

            case ObjectTypeCode.Dictionary:
                WriteDictionary(writer, objectType, obj);
                break;

            default:
                throw new NotSupportedException();
            }
        }