CSharp · Console
using System;
using System.Runtime.InteropServices;
//The Struct needs to be annotated as "Explicit Layout", like this:
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct IpAddress
{
//The "FieldOffset:" means that this Integer starts
//a offset 0, in bytes, (with sizeof(int) = 4 bytes length):
[System.Runtime.InteropServices.FieldOffset(0)]
public int intAddress;
//Offset 0, (length sizeof(byte) = 1 byte)...
[System.Runtime.InteropServices.FieldOffset(0)]
public byte Byte1;
//Offset 1, (length sizeof(byte) = 1 byte)...
[System.Runtime.InteropServices.FieldOffset(1)]
public byte Byte2;
//Offset 2, (length sizeof(byte) = 1 byte)...
[System.Runtime.InteropServices.FieldOffset(2)]
public byte Byte3;
//Offset 3, (length sizeof(byte) = 1 byte)...
[System.Runtime.InteropServices.FieldOffset(3)]
public byte Byte4;
public IpAddress(int address) {
//All elements have to be initialized. it is a good idea to initialize the
//ones not being explicitly set first.
Byte1 = Byte2 = Byte3 = Byte4 = 0;
//Now, when we init the Int, the Bytes will change too.
//If we had done this backwards, it would zero out the intAddress.
intAddress = address;
}
//Now we can use the explicit layout to access the
//bytes separately, without doing any conversion
public override string ToString() {
return (new System.Text.StringBuilder()).Append(Byte1).Append('.')
.Append(Byte2).Append('.')
.Append(Byte3).Append('.')
.Append(Byte4).ToString();
}
}
//Now let's see if we can fit a whole URL into a long
//Let's define a short enum to hold protocols
enum Protocol : short {http,https,ftp,sftp,tcp};
//The Service Struct will hold the Address, the Port and the Protocol
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct Service
{
[System.Runtime.InteropServices.FieldOffset(0)]
public IpAddress Address;
[System.Runtime.InteropServices.FieldOffset(4)]
public ushort Port;
[System.Runtime.InteropServices.FieldOffset(6)]
public Protocol AppProtocol;
[System.Runtime.InteropServices.FieldOffset(0)]
public long Payload;
public Service(IpAddress addr, ushort port, Protocol prot) {
Payload = 0; //Fields not set by the constructor must be init first
Address = addr;
Port = port;
AppProtocol = prot;
}
public Service(long val) {
//Defaults:
Address = new IpAddress(0);
Port = 80;
AppProtocol = Protocol.http;
//Setting the argument
Payload = val;
}
public Service Copy() {
return new Service(Payload);
}
public override string ToString() {
return AppProtocol + "//" + Address + ":" + Port + "/";
}
}
public class MainClass {
public static void Main()
{
var ip = new IpAddress((new Random()).Next());
Console.Out.WriteLine( "Size (bytes): " + Marshal.SizeOf(ip) +
". Value:\t\t" + ip.intAddress + "\t=\t" + ip);
var s1 = new Service(ip,8080,Protocol.https);
var s2 = new Service(s1.Payload);
s2.Address.Byte1 = 100;
s2.AppProtocol = Protocol.ftp;
Console.Out.WriteLine("Size (bytes): " + Marshal.SizeOf(s1) +
". Value: " + s1.Payload + "\t=\t" + s1);
Console.Out.WriteLine("Size (bytes): " + Marshal.SizeOf(s2) +
". Value: " + s2.Payload + "\t=\t" + s2);
}
}