/// <summary> /// Marshal a struct to managed byte array. /// </summary> /// <typeparam name="T">Type of struct.</typeparam> /// <param name="t">A struct to marshal.</param> /// <returns>Marshalled managed byte array.</returns> public static byte[] ToBytes <T>(T t) where T : struct { int size; using (MessageUtils utils = new MessageUtils(null)) { size = utils.GetSize(t); } // The size returned by MessageUtils.GetSize is not always // the actual block size of the given structure, // so allocate a buffer whose size is twice as large as it. // The actual size will be calculated later. byte[] buf = new byte[size * 2]; using (MemoryStream memoryStream = new MemoryStream(buf)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); channel.Write <T>(t); channel.EndWriteGroup(); int actualSize = (int)channel.Stream.Position; byte[] actualBuf = new byte[actualSize]; Array.Copy(buf, 0, actualBuf, 0, actualSize); return(actualBuf); } } }
/// <summary> /// Marshal a structure to managed byte array. /// </summary> /// <typeparam name="T">Type of struct.</typeparam> /// <param name="t">A structure to marshal.</param> /// <returns>Marshalled managed byte array.</returns> public static byte[] ToBytes <T>(T t) where T : struct { int size; using (MessageUtils utils = new MessageUtils(null)) { size = utils.GetSize(t); } byte[] buf = new byte[size]; using (MemoryStream memoryStream = new MemoryStream(buf)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); channel.Write <T>(t); channel.EndWriteGroup(); int actualSize = (int)channel.Stream.Position; return(ArrayUtility.SubArray(buf, 0, actualSize)); } } }