is there something equivalent to 'Address of' or offset operator in .net?

Posted by Gio on Stack Overflow See other posts from Stack Overflow or by Gio
Published on 2010-05-10T18:48:18Z Indexed on 2010/05/10 18:54 UTC
Read the original article Hit count: 174

Filed under:
|
|

We have nested stuctures as such, used as an interface for some device drivers. On occasion we have to update individual elements. An 'address of' operator would be helpful, but an 'offset' function or operator is what I'm really looking for, but not sure how to go about it. In other words, how far is structureN.elementX away from the start of the structure in bytes?

[StructLayout(LayoutKind.Sequential)]
public struct s1
{
    UInt16 elem1;   
    UInt16 elem2;
    UInt16 elem3;
}

[StructLayout(LayoutKind.Sequential)]
public struct s2
{
    UInt16 elem1;   
    UInt16 elem2;
    UInt16 elem3;
}

[StructLayout(LayoutKind.Sequential)]
public struct driver
{
    public S1 s1;
    public S2 s2;
}

For instance we need to send the device driver some data to update driver.s1.elem3, by way of providing an offset address, data block and length. We would update our local copy, then call the device api with the afore mentioned data. Not sure I have to do this with 'unsafe' method calls. Any help?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about structure