public static byte[] ToByteArray(this HierarchyId id) { if (id.Equals(null)) { return(null); } using (var stream = new MemoryStream()) { using (var writer = new BinaryWriter(stream)) { id.Write(writer); return(stream.ToArray()); } } }