淡月无痕 發表於 2023-3-23 23:04:00

.NET生成MongoDB中的主键ObjectId

<h2>前言</h2>
<p>因为很多场景下我们需要在创建MongoDB数据的时候提前生成好主键,像在EF中我们可以通过Guid.NewGuid()来生成主键,本来想着要不要实现一套MongoDB中ObjectId的,结果发现网上各种各样的实现都有,不过好在阅读C#MongoDB驱动<span class="mr-2 flex-self-stretch">mongo-csharp-driver代码的时候发现有ObjectId.GenerateNewId()的方法提供,我们可以直接调用即可,不需要我们在花费多余的时间设计重写了。</span></p>
<h2>MongoDB ObjectId类型概述</h2>
<blockquote>
<p>&nbsp;每次插入一条数据系统都会自动插入一个_id键,键值不可以重复,它可以是任何类型的,也可以手动的插入,默认情况下它的数据类型是ObjectId,由于MongoDB在设计之初就是用作分布式数据库,所以使用ObjectId可以避免不同数据库中_id的重复(如果使用自增的方式在分布式系统中就会出现重复的_id的值)。<br>ObjectId使用12字节的存储空间,每个字节可以存储两个十六进制数字,所以一共可以存储24个十六进制数字组成的字符串,在这24个字符串中,前8位表示时间戳,接下来6位是一个机器码,接下来4位表示进程id,最后6位表示计数器。</p>


</blockquote>
<p>MongoDB 采用 ObjectId 来表示主键的类型,数据库中每个文档都拥有一个_id 字段表示主键,_id 的生成规则如下:</p>
<blockquote>
<p>其中包括:4-byte Unix 时间戳,3-byte 机器 ID,2-byte 进程 ID,3-byte 计数器(初始化随机)。</p>


</blockquote>
<p><img src="https://img2023.cnblogs.com/blog/1336199/202303/1336199-20230327213834087-2092438723.png" alt="" loading="lazy"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">601e2b6ba3203cc89f   2d31aa
   ↑      ↑       ↑       ↑
时间戳    机器码   进程ID   随机数</span></pre>
</div>
<h2>MongoDB.Driver驱动安装</h2>
<h3>1、直接命令自动安装</h3>
<div class="cnblogs_code">
<pre>Install-Package MongoDB.Driver</pre>
</div>
<h3>2、搜索Nuget手动安装</h3>
<p><img src="https://img2023.cnblogs.com/blog/1336199/202303/1336199-20230301003335511-1864259395.png" alt="" class="medium-zoom-image" loading="lazy"></p>
<div>
<h2>调用生成主键ObjectId</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> primarykeyId = ObjectId.GenerateNewId();<br><span style="color: rgba(0, 128, 0, 1)">//输出:</span><span style="color: rgba(0, 128, 0, 1)">641c54b2e674000035001dc2</span></pre>
</div>
<h2>mongo-csharp-driver ObjectId详解</h2>
<blockquote>
<p>关于ObjectId的生成原理大家阅读如下源码即可。</p>
</blockquote>
<p>源码地址:https://github.com/mongodb/mongo-csharp-driver/blob/ec74978f7e827515f29cc96fba0c727828e8df7c/src/MongoDB.Bson/ObjectModel/ObjectId.cs</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* </span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://www.apache.org/licenses/LICENSE-2.0</span><span style="color: rgba(0, 128, 0, 1)">
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Diagnostics;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Runtime.CompilerServices;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Security;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Threading;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> MongoDB.Bson
{
    </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
    <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Represents an ObjectId (see also BsonObjectId).
    </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">#if</span> NET45<span style="color: rgba(0, 0, 0, 1)">
   
</span><span style="color: rgba(0, 0, 255, 1)">#endif</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">struct</span> ObjectId : IComparable&lt;ObjectId&gt;, IEquatable&lt;ObjectId&gt;<span style="color: rgba(0, 0, 0, 1)">, IConvertible
    {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> private static fields</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> ObjectId __emptyInstance = <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">(ObjectId);
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> <span style="color: rgba(0, 0, 255, 1)">int</span> __staticMachine = (GetMachineHash() + GetAppDomainId()) &amp; <span style="color: rgba(128, 0, 128, 1)">0x00ffffff</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> <span style="color: rgba(0, 0, 255, 1)">short</span> __staticPid =<span style="color: rgba(0, 0, 0, 1)"> GetPid();
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> __staticIncrement = (<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Random()).Next();

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> private fields</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> _a;
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> _b;
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> _c;

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> constructors</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Initializes a new instance of the ObjectId class.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="bytes"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The bytes.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> ObjectId(<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] bytes)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (bytes == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentNullException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bytes</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (bytes.Length != <span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Byte array must be 12 bytes long</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bytes</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            FromByteArray(bytes, </span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(0, 0, 255, 1)">out</span> _a, <span style="color: rgba(0, 0, 255, 1)">out</span> _b, <span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)"> _c);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Initializes a new instance of the ObjectId class.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="bytes"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The bytes.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="index"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The index into the byte array where the ObjectId starts.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">internal</span> ObjectId(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> index)
      {
            FromByteArray(bytes, index, </span><span style="color: rgba(0, 0, 255, 1)">out</span> _a, <span style="color: rgba(0, 0, 255, 1)">out</span> _b, <span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)"> _c);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Initializes a new instance of the ObjectId class.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The timestamp (expressed as a DateTime).</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="machine"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The machine hash.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="pid"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The PID.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="increment"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The increment.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> ObjectId(DateTime timestamp, <span style="color: rgba(0, 0, 255, 1)">int</span> machine, <span style="color: rgba(0, 0, 255, 1)">short</span> pid, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> increment)
            : </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">(GetTimestampFromDateTime(timestamp), machine, pid, increment)
      {
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Initializes a new instance of the ObjectId class.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The timestamp.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="machine"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The machine hash.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="pid"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The PID.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="increment"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The increment.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> ObjectId(<span style="color: rgba(0, 0, 255, 1)">int</span> timestamp, <span style="color: rgba(0, 0, 255, 1)">int</span> machine, <span style="color: rgba(0, 0, 255, 1)">short</span> pid, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> increment)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((machine &amp; <span style="color: rgba(128, 0, 128, 1)">0xff000000</span>) != <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentOutOfRangeException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">machine</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">The machine value must be between 0 and 16777215 (it must fit in 3 bytes).</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((increment &amp; <span style="color: rgba(128, 0, 128, 1)">0xff000000</span>) != <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentOutOfRangeException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">increment</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">The increment value must be between 0 and 16777215 (it must fit in 3 bytes).</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            _a </span>=<span style="color: rgba(0, 0, 0, 1)"> timestamp;
            _b </span>= (machine &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) | (((<span style="color: rgba(0, 0, 255, 1)">int</span>)pid &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">);
            _c </span>= ((<span style="color: rgba(0, 0, 255, 1)">int</span>)pid &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">24</span>) |<span style="color: rgba(0, 0, 0, 1)"> increment;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Initializes a new instance of the ObjectId class.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="value"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The value.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> ObjectId(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> value)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (value == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentNullException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            </span><span style="color: rgba(0, 0, 255, 1)">var</span> bytes =<span style="color: rgba(0, 0, 0, 1)"> BsonUtils.ParseHexString(value);
            FromByteArray(bytes, </span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(0, 0, 255, 1)">out</span> _a, <span style="color: rgba(0, 0, 255, 1)">out</span> _b, <span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)"> _c);
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public static properties</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets an instance of ObjectId where the value is empty.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId Empty
      {
            </span><span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> __emptyInstance; }
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public properties</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the timestamp.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> Timestamp
      {
            </span><span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> _a; }
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the machine.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> Machine
      {
            </span><span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span> (_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0xffffff</span><span style="color: rgba(0, 0, 0, 1)">; }
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the PID.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> Pid
      {
            </span><span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span> (<span style="color: rgba(0, 0, 255, 1)">short</span>)(((_b &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0xff00</span>) | ((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x00ff</span><span style="color: rgba(0, 0, 0, 1)">)); }
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the increment.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> Increment
      {
            </span><span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span> _c &amp; <span style="color: rgba(128, 0, 128, 1)">0xffffff</span><span style="color: rgba(0, 0, 0, 1)">; }
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the creation time (derived from the timestamp).
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> DateTime CreationTime
      {
            </span><span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> BsonConstants.UnixEpoch.AddSeconds(Timestamp); }
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public operators</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares two ObjectIds.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The first ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the first ObjectId is less than the second ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> <span style="color: rgba(0, 0, 255, 1)">operator</span> &lt;<span style="color: rgba(0, 0, 0, 1)">(ObjectId lhs, ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> lhs.CompareTo(rhs) &lt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares two ObjectIds.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The first ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the first ObjectId is less than or equal to the second ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> <span style="color: rgba(0, 0, 255, 1)">operator</span> &lt;=<span style="color: rgba(0, 0, 0, 1)">(ObjectId lhs, ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> lhs.CompareTo(rhs) &lt;= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares two ObjectIds.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The first ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the two ObjectIds are equal.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> <span style="color: rgba(0, 0, 255, 1)">operator</span> ==<span style="color: rgba(0, 0, 0, 1)">(ObjectId lhs, ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> lhs.Equals(rhs);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares two ObjectIds.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The first ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the two ObjectIds are not equal.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> <span style="color: rgba(0, 0, 255, 1)">operator</span> !=<span style="color: rgba(0, 0, 0, 1)">(ObjectId lhs, ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> !(lhs ==<span style="color: rgba(0, 0, 0, 1)"> rhs);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares two ObjectIds.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The first ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the first ObjectId is greather than or equal to the second ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> <span style="color: rgba(0, 0, 255, 1)">operator</span> &gt;=<span style="color: rgba(0, 0, 0, 1)">(ObjectId lhs, ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> lhs.CompareTo(rhs) &gt;= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares two ObjectIds.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The first ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the first ObjectId is greather than the second ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> <span style="color: rgba(0, 0, 255, 1)">operator</span> &gt;<span style="color: rgba(0, 0, 0, 1)">(ObjectId lhs, ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> lhs.CompareTo(rhs) &gt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public static methods</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Generates a new ObjectId with a unique value.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">An ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId GenerateNewId()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> GenerateNewId(GetTimestampFromDateTime(DateTime.UtcNow));
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Generates a new ObjectId with a unique value (with the timestamp component based on a given DateTime).
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The timestamp component (expressed as a DateTime).</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">An ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId GenerateNewId(DateTime timestamp)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> GenerateNewId(GetTimestampFromDateTime(timestamp));
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Generates a new ObjectId with a unique value (with the given timestamp).
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The timestamp component.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">An ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> ObjectId GenerateNewId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> timestamp)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> increment = Interlocked.Increment(<span style="color: rgba(0, 0, 255, 1)">ref</span> __staticIncrement) &amp; <span style="color: rgba(128, 0, 128, 1)">0x00ffffff</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> only use low order 3 bytes</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId(timestamp, __staticMachine, __staticPid, increment);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Packs the components of an ObjectId into a byte array.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The timestamp.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="machine"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The machine hash.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="pid"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The PID.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="increment"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The increment.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">A byte array.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] Pack(<span style="color: rgba(0, 0, 255, 1)">int</span> timestamp, <span style="color: rgba(0, 0, 255, 1)">int</span> machine, <span style="color: rgba(0, 0, 255, 1)">short</span> pid, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> increment)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((machine &amp; <span style="color: rgba(128, 0, 128, 1)">0xff000000</span>) != <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentOutOfRangeException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">machine</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">The machine value must be between 0 and 16777215 (it must fit in 3 bytes).</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((increment &amp; <span style="color: rgba(128, 0, 128, 1)">0xff000000</span>) != <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentOutOfRangeException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">increment</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">The increment value must be between 0 and 16777215 (it must fit in 3 bytes).</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">];
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">0</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(timestamp &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">1</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(timestamp &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">2</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(timestamp &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">3</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(timestamp);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">4</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(machine &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">5</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(machine &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">6</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(machine);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">7</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(pid &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">8</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(pid);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">9</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(increment &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">10</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(increment &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            bytes[</span><span style="color: rgba(128, 0, 128, 1)">11</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(increment);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bytes;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Parses a string and creates a new ObjectId.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="s"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The string value.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">A ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> ObjectId Parse(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> s)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (s == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentNullException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">s</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            ObjectId objectId;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (TryParse(s, <span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)"> objectId))
            {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> objectId;
            }
            </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">var</span> message = <span style="color: rgba(0, 0, 255, 1)">string</span>.Format(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">'{0}' is not a valid 24 digit hex string.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, s);
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FormatException(message);
            }
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Tries to parse a string and create a new ObjectId.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="s"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The string value.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="objectId"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The new ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the string was parsed successfully.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> TryParse(<span style="color: rgba(0, 0, 255, 1)">string</span> s, <span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId objectId)
      {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> don't throw ArgumentNullException if s is null</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span> (s != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp; s.Length == <span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] bytes;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (BsonUtils.TryParseHexString(s, <span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)"> bytes))
                {
                  objectId </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId(bytes);
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
                }
            }

            objectId </span>= <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">(ObjectId);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Unpacks a byte array into the components of an ObjectId.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="bytes"&gt;</span><span style="color: rgba(0, 128, 0, 1)">A byte array.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The timestamp.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="machine"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The machine hash.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="pid"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The PID.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="increment"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The increment.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> Unpack(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">int</span> timestamp, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">int</span> machine, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">short</span> pid, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> increment)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (bytes == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentNullException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bytes</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (bytes.Length != <span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentOutOfRangeException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bytes</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Byte array must be 12 bytes long.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            timestamp </span>= (bytes[<span style="color: rgba(128, 0, 128, 1)">0</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">24</span>) + (bytes[<span style="color: rgba(128, 0, 128, 1)">1</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">16</span>) + (bytes[<span style="color: rgba(128, 0, 128, 1)">2</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) + bytes[<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">];
            machine </span>= (bytes[<span style="color: rgba(128, 0, 128, 1)">4</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">16</span>) + (bytes[<span style="color: rgba(128, 0, 128, 1)">5</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) + bytes[<span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">];
            pid </span>= (<span style="color: rgba(0, 0, 255, 1)">short</span>)((bytes[<span style="color: rgba(128, 0, 128, 1)">7</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) + bytes[<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">]);
            increment </span>= (bytes[<span style="color: rgba(128, 0, 128, 1)">9</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">16</span>) + (bytes[<span style="color: rgba(128, 0, 128, 1)">10</span>] &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) + bytes[<span style="color: rgba(128, 0, 128, 1)">11</span><span style="color: rgba(0, 0, 0, 1)">];
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> private static methods</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> GetAppDomainId()
      {
</span><span style="color: rgba(0, 0, 255, 1)">#if</span> NETSTANDARD1_5 || NETSTANDARD1_6
            <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">#else</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> AppDomain.CurrentDomain.Id;
</span><span style="color: rgba(0, 0, 255, 1)">#endif</span><span style="color: rgba(0, 0, 0, 1)">
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the current process id.This method exists because of how CAS operates on the call stack, checking
      </span><span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> for permissions before executing the method.Hence, if we inlined this call, the calling method would not execute
      </span><span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> before throwing an exception requiring the try/catch at an even higher level that we don't necessarily control.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
<span style="color: rgba(0, 0, 0, 1)">      
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> GetCurrentProcessId()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Process.GetCurrentProcess().Id;
      }

      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> GetMachineHash()
      {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> use instead of Dns.HostName so it will work offline</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> machineName =<span style="color: rgba(0, 0, 0, 1)"> GetMachineName();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0x00ffffff</span> &amp; machineName.GetHashCode(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> use first 3 bytes of hash</span>
<span style="color: rgba(0, 0, 0, 1)">      }

      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> GetMachineName()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Environment.MachineName;
      }

      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> GetPid()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> (<span style="color: rgba(0, 0, 255, 1)">short</span>)GetCurrentProcessId(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> use low order two bytes only</span>
<span style="color: rgba(0, 0, 0, 1)">            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SecurityException)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
            }
      }

      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> GetTimestampFromDateTime(DateTime timestamp)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> secondsSinceEpoch = (<span style="color: rgba(0, 0, 255, 1)">long</span>)Math.Floor((BsonUtils.ToUniversalTime(timestamp) -<span style="color: rgba(0, 0, 0, 1)"> BsonConstants.UnixEpoch).TotalSeconds);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (secondsSinceEpoch &lt; <span style="color: rgba(0, 0, 255, 1)">int</span>.MinValue || secondsSinceEpoch &gt; <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">.MaxValue)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentOutOfRangeException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">timestamp</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> (<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">)secondsSinceEpoch;
      }

      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> FromByteArray(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes, <span style="color: rgba(0, 0, 255, 1)">int</span> offset, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">int</span> a, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">int</span> b, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> c)
      {
            a </span>= (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">24</span>) | (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">16</span>) | (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) | bytes;
            b </span>= (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">24</span>) | (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">16</span>) | (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) | bytes;
            c </span>= (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">24</span>) | (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">16</span>) | (bytes &lt;&lt; <span style="color: rgba(128, 0, 128, 1)">8</span>) | bytes;
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public methods</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares this ObjectId to another ObjectId.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="other"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">A 32-bit signed integer that indicates whether this ObjectId is less than, equal to, or greather than the other.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> CompareTo(ObjectId other)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> result = ((<span style="color: rgba(0, 0, 255, 1)">uint</span>)_a).CompareTo((<span style="color: rgba(0, 0, 255, 1)">uint</span><span style="color: rgba(0, 0, 0, 1)">)other._a);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (result != <span style="color: rgba(128, 0, 128, 1)">0</span>) { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; }
            result </span>= ((<span style="color: rgba(0, 0, 255, 1)">uint</span>)_b).CompareTo((<span style="color: rgba(0, 0, 255, 1)">uint</span><span style="color: rgba(0, 0, 0, 1)">)other._b);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (result != <span style="color: rgba(128, 0, 128, 1)">0</span>) { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> ((<span style="color: rgba(0, 0, 255, 1)">uint</span>)_c).CompareTo((<span style="color: rgba(0, 0, 255, 1)">uint</span><span style="color: rgba(0, 0, 0, 1)">)other._c);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares this ObjectId to another ObjectId.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="rhs"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other ObjectId.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the two ObjectIds are equal.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">bool</span><span style="color: rgba(0, 0, 0, 1)"> Equals(ObjectId rhs)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
                _a </span>== rhs._a &amp;&amp;<span style="color: rgba(0, 0, 0, 1)">
                _b </span>== rhs._b &amp;&amp;<span style="color: rgba(0, 0, 0, 1)">
                _c </span>==<span style="color: rgba(0, 0, 0, 1)"> rhs._c;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Compares this ObjectId to another object.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="obj"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The other object.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">True if the other object is an ObjectId and equal to this one.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> Equals(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> obj)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (obj <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Equals((ObjectId)obj);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
            }
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Gets the hash code.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">The hash code.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> GetHashCode()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> hash = <span style="color: rgba(128, 0, 128, 1)">17</span><span style="color: rgba(0, 0, 0, 1)">;
            hash </span>= <span style="color: rgba(128, 0, 128, 1)">37</span> * hash +<span style="color: rgba(0, 0, 0, 1)"> _a.GetHashCode();
            hash </span>= <span style="color: rgba(128, 0, 128, 1)">37</span> * hash +<span style="color: rgba(0, 0, 0, 1)"> _b.GetHashCode();
            hash </span>= <span style="color: rgba(128, 0, 128, 1)">37</span> * hash +<span style="color: rgba(0, 0, 0, 1)"> _c.GetHashCode();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> hash;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Converts the ObjectId to a byte array.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">A byte array.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] ToByteArray()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> bytes = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">];
            ToByteArray(bytes, </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bytes;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Converts the ObjectId to a byte array.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="destination"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The destination.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="offset"&gt;</span><span style="color: rgba(0, 128, 0, 1)">The offset.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> ToByteArray(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] destination, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> offset)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (destination == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentNullException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">destination</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (offset + <span style="color: rgba(128, 0, 128, 1)">12</span> &gt;<span style="color: rgba(0, 0, 0, 1)"> destination.Length)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArgumentException(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Not enough room in destination buffer.</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">offset</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            }

            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(_a);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(_b);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">);
            destination = (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">)(_c);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Returns a string representation of the value.
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;</span><span style="color: rgba(0, 128, 0, 1)">A string representation of the value.</span><span style="color: rgba(128, 128, 128, 1)">&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> ToString()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> c = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">char</span>[<span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">];
            c[</span><span style="color: rgba(128, 0, 128, 1)">0</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">28</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">1</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">2</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">20</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">3</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">4</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">12</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">5</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">6</span>] = BsonUtils.ToHexChar((_a &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">4</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">7</span>] = BsonUtils.ToHexChar(_a &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">8</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">28</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">9</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">10</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">20</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">11</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">12</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">12</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">13</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">14</span>] = BsonUtils.ToHexChar((_b &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">4</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">15</span>] = BsonUtils.ToHexChar(_b &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">16</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">28</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">17</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">24</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">18</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">20</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">19</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">16</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">20</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">12</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">21</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">8</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">22</span>] = BsonUtils.ToHexChar((_c &gt;&gt; <span style="color: rgba(128, 0, 128, 1)">4</span>) &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            c[</span><span style="color: rgba(128, 0, 128, 1)">23</span>] = BsonUtils.ToHexChar(_c &amp; <span style="color: rgba(128, 0, 128, 1)">0x0f</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">(c);
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> explicit IConvertible implementation</span>
<span style="color: rgba(0, 0, 0, 1)">      TypeCode IConvertible.GetTypeCode()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> TypeCode.Object;
      }

      </span><span style="color: rgba(0, 0, 255, 1)">bool</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToBoolean(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToByte(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">char</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToChar(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      DateTime IConvertible.ToDateTime(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">decimal</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToDecimal(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToDouble(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToInt16(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToInt32(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToInt64(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">sbyte</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToSByte(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">float</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToSingle(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToString(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ToString();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToType(Type conversionType, IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (Type.GetTypeCode(conversionType))
            {
                </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> TypeCode.String:
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span> ((IConvertible)<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).ToString(provider);
                </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> TypeCode.Object:
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (conversionType == <span style="color: rgba(0, 0, 255, 1)">typeof</span>(<span style="color: rgba(0, 0, 255, 1)">object</span>) || conversionType == <span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(ObjectId))
                  {
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (conversionType == <span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(BsonObjectId))
                  {
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> BsonObjectId(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
                  }
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (conversionType == <span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(BsonString))
                  {
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> BsonString(((IConvertible)<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).ToString(provider));
                  }
                  </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
            }

            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">ushort</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToUInt16(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">uint</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToUInt32(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">ulong</span><span style="color: rgba(0, 0, 0, 1)"> IConvertible.ToUInt64(IFormatProvider provider)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvalidCastException();
      }
    }
}</span></pre>
</div>
</div>

</div>
<div id="MySignature" role="contentinfo">
    <blockquote >
<p style='font-family:YouYuan;font-size: 16px;margin: 0 auto 0.01em auto;'><span style='font-size: 17px; '>作者名称:</span>追逐时光者</p>
<p style='font-family:YouYuan;font-size: 16px;margin: 0 auto 0.01em auto;'><span style='font-size: 17px; '>作者简介:</span>一个热爱编程、善于分享、喜欢学习、探索、尝试新事物和新技术的全栈软件工程师。</p>
<p style='font-family:YouYuan;font-size: 16px;margin: 0 auto 0.01em auto;'>
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。如果该篇文章对您有帮助的话,可以点一下右下角的【&hearts;推荐&hearts;】,希望能够持续的为大家带来好的技术文章,文中可能存在描述不正确的地方,欢迎指正或补充,不胜感激。
</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/Can-daydayup/p/17249852.html
頁: [1]
查看完整版本: .NET生成MongoDB中的主键ObjectId