Skip to content

Instantly share code, notes, and snippets.

@creyke
Created October 18, 2017 22:49
Show Gist options
  • Save creyke/08d42d05c4c561cf1ba8e63a094afbeb to your computer and use it in GitHub Desktop.
Save creyke/08d42d05c4c561cf1ba8e63a094afbeb to your computer and use it in GitHub Desktop.
Example packed Guid
public class TenantAndUserKey : PackedGuidKey
{
public TenantAndUserKey()
{
}
public TenantAndUserKey(Guid guid) : base(guid)
{
}
public uint TenantId { get { return GetUint(0); } set { Set(value, 0); } }
public int UserId { get { return GetInt(4); } set { Set(value, 4); } }
}
public abstract class PackedGuidKey
{
private byte[] bytes;
public Guid Guid { get; private set; }
public PackedGuidKey()
{
bytes = new byte[16];
GenerateGuid();
}
public PackedGuidKey(Guid guid)
{
bytes = guid.ToByteArray();
GenerateGuid();
}
private void GenerateGuid()
{
Guid = new Guid(bytes);
}
protected int GetInt(int startPosition)
{
return BitConverter.ToInt32(bytes, startPosition);
}
protected uint GetUint(int startPosition)
{
return BitConverter.ToUInt32(bytes, startPosition);
}
protected void Set(int value, int startPosition)
{
Array.Copy(BitConverter.GetBytes(value), 0, bytes, startPosition, sizeof(int));
GenerateGuid();
}
protected void Set(uint value, int startPosition)
{
Array.Copy(BitConverter.GetBytes(value), 0, bytes, startPosition, sizeof(uint));
GenerateGuid();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment