struct t { public int a; public char b; } unsafe static void Main(string[] args) { t objt = new t(); objt.a = 30; objt.b = 'a'; t* pt; pt = &objt; byte* pb = (byte*)pt; byte[] buf=new byte[sizeof(t)]; for (int i = 0; i < buf.Length; i++) { buf[i]=(*pb)++; } FileStream fs = File.Open("c:\\a.dat", FileMode.Create); fs.Write(buf, 0, buf.Length); fs.Flush(); fs.Close();
Console.Read(); }
[StructLayout(LayoutKind.Sequential)] public struct Point { public int x; public int y; }.... Point p1 = new Point(); System.IntPtr p = Marshal.AllocHGlobal( Marshal.SizeOf( p1 ) ); Marshal.StructureToPtr( p1 , p , true ); byte[] b = new byte[Marshal.SizeOf(p1)]; for( int i=0 ; i<Marshal.SizeOf( p1 ) ; i++ ) { b[i] = Marshal.ReadByte( b ,i ); }
{
public int a;
public char b;
} unsafe static void Main(string[] args)
{
t objt = new t();
objt.a = 30;
objt.b = 'a';
t* pt;
pt = &objt;
byte* pb = (byte*)pt;
byte[] buf=new byte[sizeof(t)];
for (int i = 0; i < buf.Length; i++)
{
buf[i]=(*pb)++;
} FileStream fs = File.Open("c:\\a.dat", FileMode.Create);
fs.Write(buf, 0, buf.Length);
fs.Flush();
fs.Close();
Console.Read();
}
public struct Point {
public int x;
public int y;
}....
Point p1 = new Point();
System.IntPtr p = Marshal.AllocHGlobal( Marshal.SizeOf( p1 ) );
Marshal.StructureToPtr( p1 , p , true );
byte[] b = new byte[Marshal.SizeOf(p1)];
for( int i=0 ; i<Marshal.SizeOf( p1 ) ; i++ )
{
b[i] = Marshal.ReadByte( b ,i );
}