/// <summary> /// The GetStructureBytes. /// </summary> /// <param name="structure">The structure<see cref="object"/>.</param> /// <returns>The <see cref="byte[]"/>.</returns> public unsafe static byte[] GetStructureBytes(object structure) { byte[] b = null; if (structure is ValueType) { Type t = structure.GetType(); if (t.IsPrimitive || t.IsLayoutSequential) { return(extractor.ValueStructureToBytes(structure)); } if (structure is DateTime) { b = new byte[8]; structure = ((DateTime)structure).ToBinary(); } else if (structure is Enum) { b = new byte[4]; structure = Convert.ToInt32((Enum)structure); } else { b = new byte[Marshal.SizeOf(structure)]; } } else if (structure.GetType() == typeof(string)) { int l = ((string)structure).Length; b = new byte[l]; fixed(char *c = (string)structure) fixed(byte *pb = b) CopyBlock(pb, (byte *)c, l); return(b); } else { b = new byte[Marshal.SizeOf(structure)]; fixed(byte *pb = b) Marshal.StructureToPtr(structure, new IntPtr(pb), false); return(b); }
/// <summary> /// The ValueStructureToBytes. /// </summary> /// <param name="structure">The structure<see cref="object"/>.</param> /// <returns>The <see cref="byte[]"/>.</returns> public static byte[] ValueStructureToBytes(object structure) { return(_extract.ValueStructureToBytes(structure)); }