Esempio n. 1
0
 private static void ValidateEnumValue(Type enumType, object value)
 {
     if (!EnumTools.IsValid(enumType, value))
     {
         throw new InvalidDataException($"Read value {value} is not defined in the enum type {enumType}.");
     }
 }
Esempio n. 2
0
        private static object ReadEnum(Stream stream, Type enumType, bool strict, ByteConverter converter)
        {
            converter = converter ?? ByteConverter.System;

            Type   valueType = Enum.GetUnderlyingType(enumType);
            int    valueSize = Marshal.SizeOf(valueType);
            object value;

            // Read enough bytes to form an enum value.
            FillBuffer(stream, valueSize);
            if (valueType == typeof(Byte))
            {
                value = Buffer[0];
            }
            else if (valueType == typeof(SByte))
            {
                value = (sbyte)Buffer[0];
            }
            else if (valueType == typeof(Int16))
            {
                value = converter.ToInt16(Buffer);
            }
            else if (valueType == typeof(Int32))
            {
                value = converter.ToInt32(Buffer);
            }
            else if (valueType == typeof(Int64))
            {
                value = converter.ToInt64(Buffer);
            }
            else if (valueType == typeof(UInt16))
            {
                value = converter.ToUInt16(Buffer);
            }
            else if (valueType == typeof(UInt32))
            {
                value = converter.ToUInt32(Buffer);
            }
            else if (valueType == typeof(UInt64))
            {
                value = converter.ToUInt64(Buffer);
            }
            else
            {
                throw new NotImplementedException($"Unsupported enum type {valueType}.");
            }

            // Check if the value is defined in the enumeration, if requested.
            if (strict)
            {
                EnumTools.IsValid(enumType, value);
            }
            return(value);
        }
Esempio n. 3
0
        // ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------

        private static void WriteEnum(Stream stream, Type enumType, object value, bool strict, ByteConverter converter)
        {
            converter = converter ?? ByteConverter.System;
            Type valueType = Enum.GetUnderlyingType(enumType);

            // Write the enum value.
            byte[] buffer = Buffer;
            if (valueType == typeof(Byte))
            {
                Buffer[0] = (byte)value;
            }
            else if (valueType == typeof(SByte))
            {
                Buffer[0] = (byte)(sbyte)value;
            }
            else if (valueType == typeof(Int16))
            {
                converter.GetBytes((Int16)value, buffer, 0);
            }
            else if (valueType == typeof(Int32))
            {
                converter.GetBytes((Int32)value, buffer, 0);
            }
            else if (valueType == typeof(Int64))
            {
                converter.GetBytes((Int64)value, buffer, 0);
            }
            else if (valueType == typeof(UInt16))
            {
                converter.GetBytes((UInt16)value, buffer, 0);
            }
            else if (valueType == typeof(UInt32))
            {
                converter.GetBytes((UInt32)value, buffer, 0);
            }
            else if (valueType == typeof(UInt64))
            {
                converter.GetBytes((UInt64)value, buffer, 0);
            }
            else
            {
                throw new NotImplementedException($"Unsupported enum type {valueType}.");
            }

            // Check if the value is defined in the enumeration, if requested.
            if (strict)
            {
                EnumTools.IsValid(enumType, value);
            }
            stream.Write(buffer, 0, Marshal.SizeOf(valueType));
        }