public unsafe void Extractor_StructModel_Test() { Assemblies.ResolveExecuting(); StructModel[] structure = new StructModel[] { new StructModel(83948930), new StructModel(45453), new StructModel(5435332) }; structure[0].Alias = "FirstAlias"; structure[0].Name = "FirstName"; structure[1].Alias = "SecondAlia"; structure[1].Name = "SecondName"; structure[2].Alias = "ThirdAlias"; structure[2].Name = "ThirdName"; StructModels structures = new StructModels(structure); int size = Marshal.SizeOf(structure[0]); byte *pserial = Extraction.ValueStructureToPointer(structure[0]); StructModel structure2 = new StructModel(); ValueType o = structure2; o = Extraction.PointerToValueStructure(pserial, o, 0); structure2 = (StructModel)o; structure2.Alias = "FirstChange"; }
/// <summary> /// The BytesToStructure. /// </summary> /// <param name="binary">The binary<see cref="byte[]"/>.</param> /// <param name="structure">The structure<see cref="object"/>.</param> /// <param name="offset">The offset<see cref="long"/>.</param> /// <returns>The <see cref="object"/>.</returns> public unsafe static object BytesToStructure(byte[] binary, object structure, long offset) { if (structure is ValueType) { return(Extraction.BytesToValueStructure(binary, structure, 0)); } else { fixed(byte *b = &binary[offset]) return(PointerToStructure(new IntPtr(b), structure)); } }
/// <summary> /// The GetBytes. /// </summary> /// <param name="objvalue">The objvalue<see cref="Object"/>.</param> /// <param name="forKeys">The forKeys<see cref="bool"/>.</param> /// <returns>The <see cref="Byte[]"/>.</returns> public unsafe static Byte[] GetBytes(this Object objvalue, bool forKeys = false) { Type t = objvalue.GetType(); if (objvalue is IUnique) { if (forKeys) { return(((IUnique)objvalue).GetUniqueBytes()); } return(((IUnique)objvalue).GetBytes()); } if (objvalue is IList) { return(((IList)objvalue).GetBytes(forKeys)); } if (t.IsValueType) { if (t.IsPrimitive) { return(Extraction.ValueStructureToBytes(objvalue)); } if (objvalue is DateTime) { return(((DateTime)objvalue).ToBinary().GetBytes()); } if (objvalue is Enum) { return(Convert.ToInt32(objvalue).GetBytes()); } return(objvalue.GetStructureBytes()); } if (t.IsLayoutSequential) { return(objvalue.GetSequentialBytes()); } if (objvalue is String || objvalue is IFormattable) { return(((string)objvalue).GetBytes()); } return(new byte[0]); }
/// <summary> /// The PointerToStructure. /// </summary> /// <param name="binary">The binary<see cref="IntPtr"/>.</param> /// <param name="structure">The structure<see cref="object"/>.</param> /// <returns>The <see cref="object"/>.</returns> public unsafe static object PointerToStructure(IntPtr binary, object structure) { if (structure is ValueType) { Type t = structure.GetType(); if (t.IsLayoutSequential) { return(Extraction.PointerToValueStructure((byte *)(binary.ToPointer()), structure, 0)); } else { return(PointerToStructure(binary, structure.GetType(), 0)); } } else { Marshal.PtrToStructure(binary, structure); } return(structure); }
/// <summary> /// The PointerToStructure. /// </summary> /// <param name="binary">The binary<see cref="byte*"/>.</param> /// <param name="structure">The structure<see cref="ValueType"/>.</param> /// <returns>The <see cref="ValueType"/>.</returns> public unsafe static ValueType PointerToStructure(byte *binary, ValueType structure) { return(Extraction.PointerToValueStructure(binary, structure, 0)); }
/// <summary> /// The GetValueStructureBytes. /// </summary> /// <param name="structure">The structure<see cref="object"/>.</param> /// <returns>The <see cref="byte[]"/>.</returns> public unsafe static byte[] GetValueStructureBytes(this object structure) { return(Extraction.ValueStructureToBytes(structure)); }