S
Steve Terepin
I have a binary file containing fixed-length records, which I
need to read into an array of structs. I've found a relatively
nice way to do this, roughly as follows (some details omitted) :
[StructLayout(LayoutKind.Explicit,Size=8)]
struct MyStruct {
[FieldOffset(0)] int field_A ;
[FieldOffset(4)] int field_B ;
}
unsafe void LoadRecord ( int jRecord, void * pStruct, int nBytes )
{
// Reads 'nBytes' bytes of data from the specified record in the file, and
// writes them to the address passed in, ie replacing the current contents
// of the struct
}
MyStruct GetMyStruct ( int jRecord )
{
MyStruct x ;
LoadRecord(jRecord,&x,8) ;
return x ;
}
This works OK, but what I'd really like to have is a completely generic
routine
that could be passed the Type of the struct, and return a newly created
instance
of that type which has been initialised by overwriting the struct fields
with data
from the file. Something like :
object GetMyGenericStruct ( int jRecord, System.Type type )
{
object tmp = System.Activator.CreateInstance(type) ;
int nBytes = System.Runtime.InteropServices.Marshal.SizeOf(type) ;
// Assuming that 'type' is typeof(MyStruct), we now have
// a 'boxed' instance of MyStruct. Problem is, how to determine
// the address of the beginning of the struct's data ?
fixed ( void * p = ?? )
{
LoadRecord(jRecord,p,nBytes) ;
}
return tmp ;
}
Is there a safe and portable way to find the correct address ?
If not, I'll just have to wait until Generics come along ... or hard code
a 'GetMyStruct' routine for each type.
Steve.
need to read into an array of structs. I've found a relatively
nice way to do this, roughly as follows (some details omitted) :
[StructLayout(LayoutKind.Explicit,Size=8)]
struct MyStruct {
[FieldOffset(0)] int field_A ;
[FieldOffset(4)] int field_B ;
}
unsafe void LoadRecord ( int jRecord, void * pStruct, int nBytes )
{
// Reads 'nBytes' bytes of data from the specified record in the file, and
// writes them to the address passed in, ie replacing the current contents
// of the struct
}
MyStruct GetMyStruct ( int jRecord )
{
MyStruct x ;
LoadRecord(jRecord,&x,8) ;
return x ;
}
This works OK, but what I'd really like to have is a completely generic
routine
that could be passed the Type of the struct, and return a newly created
instance
of that type which has been initialised by overwriting the struct fields
with data
from the file. Something like :
object GetMyGenericStruct ( int jRecord, System.Type type )
{
object tmp = System.Activator.CreateInstance(type) ;
int nBytes = System.Runtime.InteropServices.Marshal.SizeOf(type) ;
// Assuming that 'type' is typeof(MyStruct), we now have
// a 'boxed' instance of MyStruct. Problem is, how to determine
// the address of the beginning of the struct's data ?
fixed ( void * p = ?? )
{
LoadRecord(jRecord,p,nBytes) ;
}
return tmp ;
}
Is there a safe and portable way to find the correct address ?
If not, I'll just have to wait until Generics come along ... or hard code
a 'GetMyStruct' routine for each type.
Steve.