public byte[] ConvertToBytes(object instance, bool includeLength)
        {
            Enum e = (Enum)instance;
            Type underlyingType = e.GetType().GetEnumUnderlyingType();

            if (!ParentModule.HasConverterOfType(underlyingType))
            {
                NetBase.WriteDebug($"Invalid element type for enum conversion! ({underlyingType})", true);
            }

            List <byte> data = new List <byte>();

            data.AddRange(ParentModule.ConvertToBytes(Convert.ChangeType(e, underlyingType), includeLength));

            return(data.ToArray());
        }
Exemplo n.º 2
0
        public byte[] ConvertToBytes(object instance, bool includeLength)
        {
            string s = (string)instance;

            byte[] utf8Bytes = Encoding.UTF8.GetBytes(s);

            if (!includeLength)
            {
                return(utf8Bytes);
            }

            byte[] lengthBytes = ParentModule.ConvertToBytes((ushort)utf8Bytes.Length);
            byte[] data        = new byte[lengthBytes.Length + utf8Bytes.Length];
            lengthBytes.CopyTo(data, 0);
            utf8Bytes.CopyTo(data, lengthBytes.Length);

            return(data);
        }
        public byte[] ConvertToBytes(object instance, bool includeLength)
        {
            Array a           = (Array)instance;
            Type  elementType = instance.GetType().GetElementType();

            if (!ParentModule.HasConverterOfType(elementType))
            {
                NetBase.WriteDebug($"Invalid element type in array conversion! ({elementType})", true);
            }

            List <byte> data = new List <byte>();

            data.AddRange(ParentModule.ConvertToBytes((ushort)a.Length));

            for (int i = 0; i < a.Length; ++i)
            {
                data.AddRange(ParentModule.ConvertToBytes(a.GetValue(i)));
            }

            return(data.ToArray());
        }