[Fact] public unsafe void Extractor_StructModel_Test() { 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 = ExtractOperation.ValueStructureToPointer(structure[0]); StructModel structure2 = new StructModel(); ValueType o = structure2; ExtractOperation.PointerToValueStructure(pserial, o, 0); structure2 = (StructModel)o; structure2.Alias = "FirstChange"; }
public unsafe static void StructureToPointer(object structure, IntPtr binary) { if (structure is ValueType) { Type t = structure.GetType(); if (t.IsPrimitive || t.IsLayoutSequential) { Marshal.StructureToPtr(structure, binary, false); return; } if (structure is DateTime) { structure = ((DateTime)structure).ToBinary(); Marshal.StructureToPtr(structure, binary, false); return; } if (t.IsLayoutSequential) { ExtractOperation.ValueStructureToPointer(structure, (byte *)binary, 0); return; } } Marshal.StructureToPtr(structure, binary, false); }
public unsafe static byte *GetStructurePointer(object structure) { int size = 0; if (structure is ValueType) { Type t = structure.GetType(); if (t.IsPrimitive || t.IsLayoutSequential) { return(ExtractOperation.ValueStructureToPointer(structure)); } if (structure is DateTime) { size = 8; structure = ((DateTime)structure).ToBinary(); } else if (structure is ISerialNumber) { size = 24; } else if (structure is IUnique) { size = 8; } else if (structure is Enum) { size = 4; structure = Convert.ToInt32((Enum)structure); } else { size = Marshal.SizeOf(structure); } } else { size = Marshal.SizeOf(structure); } IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(structure)); Marshal.StructureToPtr(structure, p, false); return((byte *)p); }