如何在 C # 中生成 UUID

我正在以编程方式创建一个. idl 文件。如何以编程方式为接口和方法创建 UUID。

我可以用编程方式生成 UUID 吗?

216138 次浏览

你可能正在寻找 System.Guid.NewGuid()

我不了解方法,但是 GUID 的类型可以通过以下方式实现:

Guid iid = System.Runtime.InteropServices.Marshal.GenerateGuidForType(typeof(IFoo));

Http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.generateguidfortype.aspx

当字符串表示为。NET 指南和(RFC4122) UUID 是相同的,存储格式不是。。NET 以小端字节交易前三个 Guid部分。

如果要传输字节(例如,作为 base64) ,则不能只使用 Guid.ToByteArray()并对其进行编码。您将需要 Array.Reverse的前三部分(Data1-3)。

我是这么做的:

var rfc4122bytes = Convert.FromBase64String("aguidthatIgotonthewire==");
Array.Reverse(rfc4122bytes,0,4);
Array.Reverse(rfc4122bytes,4,2);
Array.Reverse(rfc4122bytes,6,2);
var guid = new Guid(rfc4122bytes);

有关具体的.NET 实现细节,请参见 这个答案

编辑 : 感谢代码管理员 Jeff Walker 指出内部结构与进出 byte-array 构造函数和 ToByteArray()的字节数组格式无关。

下面是客户端的“顺序向导”解决方案。

Http://www.pinvoke.net/default.aspx/rpcrt4.uuidcreate

using System;
using System.Runtime.InteropServices;




namespace MyCompany.MyTechnology.Framework.CrossDomain.GuidExtend
{
public static class Guid
{


/*


Original Reference for Code:
http://www.pinvoke.net/default.aspx/rpcrt4/UuidCreateSequential.html


*/




[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out System.Guid guid);


public static System.Guid NewGuid()
{
return CreateSequentialUuid();
}




public static System.Guid CreateSequentialUuid()
{
const int RPC_S_OK = 0;
System.Guid g;
int hr = UuidCreateSequential(out g);
if (hr != RPC_S_OK)
throw new ApplicationException("UuidCreateSequential failed: " + hr);
return g;
}




/*


Text From URL above:


UuidCreateSequential (rpcrt4)


Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than rpcrt4, prefix the name with the module name and a period.
. Summary
Creates a new UUID
C# Signature:
[DllImport("rpcrt4.dll", SetLastError=true)]
static extern int UuidCreateSequential(out Guid guid);




VB Signature:
Declare Function UuidCreateSequential Lib "rpcrt4.dll" (ByRef id As Guid) As Integer




User-Defined Types:
None.


Notes:
Microsoft changed the UuidCreate function so it no longer uses the machine's MAC address as part of the UUID. Since CoCreateGuid calls UuidCreate to get its GUID, its output also changed. If you still like the GUIDs to be generated in sequential order (helpful for keeping a related group of GUIDs together in the system registry), you can use the UuidCreateSequential function.


CoCreateGuid generates random-looking GUIDs like these:


92E60A8A-2A99-4F53-9A71-AC69BD7E4D75
BB88FD63-DAC2-4B15-8ADF-1D502E64B92F
28F8800C-C804-4F0F-B6F1-24BFC4D4EE80
EBD133A6-6CF3-4ADA-B723-A8177B70D268
B10A35C0-F012-4EC1-9D24-3CC91D2B7122






UuidCreateSequential generates sequential GUIDs like these:


19F287B4-8830-11D9-8BFC-000CF1ADC5B7
19F287B5-8830-11D9-8BFC-000CF1ADC5B7
19F287B6-8830-11D9-8BFC-000CF1ADC5B7
19F287B7-8830-11D9-8BFC-000CF1ADC5B7
19F287B8-8830-11D9-8BFC-000CF1ADC5B7






Here is a summary of the differences in the output of UuidCreateSequential:


The last six bytes reveal your MAC address
Several GUIDs generated in a row are sequential
Tips & Tricks:
Please add some!


Sample Code in C#:
static Guid UuidCreateSequential()
{
const int RPC_S_OK = 0;
Guid g;
int hr = UuidCreateSequential(out g);
if (hr != RPC_S_OK)
throw new ApplicationException
("UuidCreateSequential failed: " + hr);
return g;
}






Sample Code in VB:
Sub Main()
Dim myId As Guid
Dim code As Integer
code = UuidCreateSequential(myId)
If code <> 0 Then
Console.WriteLine("UuidCreateSequential failed: {0}", code)
Else
Console.WriteLine(myId)
End If
End Sub








*/
















}
}

关键字: CreateSequentialUUID SequentialUUID

我在 C # 中有一个 GitHub Gist 和一个类似 UUID 的 Java 实现: Https://gist.github.com/rickbeerendonk/13655dd24ec574954366

UUID 可以从最小和最重要的位创建,就像在 Java 中一样。同时也暴露了他们。该实现具有到 GUID 的显式转换和从 GUID 的隐式转换。