Jump to content

Compound data type: Difference between revisions

Reader included
(Reader included)
Line 97:
public Point(int x0) { x = x0; y = 0; }
public Point(int x0, int y0) { x = x0; y = y0; }
}
 
public static Point ReadFromFileStream(FileStream fs)
{
// Create a buffer
byte[] buffer = new byte[Marshal.SizeOf(typeof(Point))];
// Read bytes into the buffer...
fs.Read(buffer, 0, Marshal.SizeOf(typeof(Point)) );
// Make sure that the Garbage Collector does not move the buffer
GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);
// Marshal the bytes
Point point = (Point)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Point));
// Give control of the buffer back to the Garbage Collector
handle.Free();
return point;
}
 
public static Point ReadFromBinaryReader(BinaryReader br)
{
//Read byte array
byte[] buffer = br.ReadBytes(Marshal.SizeOf(typeof(Point)));
//Make sure that the Garbage Collector doesn't move our buffer
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
//Marshal the bytes
Point point = (Point)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Point));
// Give control of the buffer back to the Garbage Collector
handle.Free();
return point;
}
 
// Improve speed by using PointSize
internal sealed class PointSize
{
public static int size;
static PointSize()
{
size = Marshal.SizeOf(typeof(Point));
}
public static int Size
{
get
{
return size;
}
}
}
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.