Sport Monkey

Adventures in code land

Welcome to Sport Monkey

Soon there shall be a Jekyll blog here, but in the mean time appreciate some generics:

        T BitConverterPlus<T>(bool isBigEndian, byte[] bytes, int startIndex = 0)
        {
            var len = bytes.Length;
            var bytesCopy = new byte[len];
            Array.Copy(bytes, bytesCopy, len);

            if (BitConverter.IsLittleEndian ? isBigEndian : !isBigEndian)
            {
                Array.Reverse(bytesCopy);
                int typeSize = System.Runtime.InteropServices.Marshal.SizeOf(default(T));
                startIndex = (len - typeSize) - startIndex;
            }
            var paramAray = new object[] { bytesCopy, startIndex };

            string typeName = typeof(T).Name;
            var methodInfo = (typeof(BitConverter)).GetMethod("To" + typeName);
            T obj = (T)methodInfo.Invoke(null, paramAray);
            return obj;
        }