This code does what you need:
using System; using System.Runtime.InteropServices; public static class SequentialGuidProvider { [DllImport("rpcrt4.dll", SetLastError = true)] private static extern int UuidCreateSequential(out Guid guid); private static Guid CreateGuid() { Guid guid; int result = UuidCreateSequential(out guid); if (result == 0) return guid; else return Guid.NewGuid(); } public static Guid GuidComb(this Nullable<Guid> guid) { if (!guid.HasValue) guid = SequentialGuidProvider.CreateGuid(); return guid.Value; } }
Testing Class:
public class TestObject { public TestObject() { } private Nullable<Guid> _guid = null; public Guid Id { get { _guid = _guid.GuidComb(); return _guid.Value(); } set { _guid = value; } } }
Test code:
static void Main(string[] args) { TestObject testObject1 = new TestObject(); TestObject testObject2 = new TestObject(); TestObject testObject3 = new TestObject();
qujck May 01 '13 at 15:41 2013-05-01 15:41
source share