Mega Code Archive

 
Categories / C# / Development Class
 

Returns the memory representation of an object

using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace Vestris.ResourceLib {     /// <summary>     /// Resource utilities.     /// </summary>     public abstract class ResourceUtil     {         /// <summary>         /// Returns the memory representation of an object.         /// </summary>         /// <typeparam name="T">Object type.</typeparam>         /// <param name="anything">Data.</param>         /// <returns>Object's representation in memory.</returns>         internal static byte[] GetBytes<T>(T anything)         {             int rawsize = Marshal.SizeOf(anything);             IntPtr buffer = Marshal.AllocHGlobal(rawsize);             Marshal.StructureToPtr(anything, buffer, false);             byte[] rawdatas = new byte[rawsize];             Marshal.Copy(buffer, rawdatas, 0, rawsize);             Marshal.FreeHGlobal(buffer);             return rawdatas;         }     } }