我正在通过程序创建一个 .idl 文件。如何通过程序创建接口和方法的 UUID。
我能否通过程序生成 UUID?
我正在通过程序创建一个 .idl 文件。如何通过程序创建接口和方法的 UUID。
我能否通过程序生成 UUID?
System.Guid.NewGuid()
。System.Guid.NewGuid().ToString("B").ToUpper()
。例如,vdproj
设置项目中的UUID是大写的,如果您提供小写的UUID,则会引发异常。 - Mark Lakata注意:虽然.NET Guid和(RFC4122) UUID的字符串表示形式相同,但它们的存储格式不同。对于前三个Guid
部分,.NET使用小端字节序。
如果您正在传输字节(例如作为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、Code Ranger指出,内部实现与进出字节数组构造函数和ToByteArray()
的格式无关。
Guid
(因为它是针对.idl文件的),但这里我遇到了这个问题。所以,这里提供给你们,必应和谷歌用户们。 - Ben MosherToByteArray
不是;它们始终是小端的。接下来进行编辑。 - Ben Mosherusing 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
*/
}
}
关键词:创建顺序UUID 顺序UUID
我在GitHub Gist上有一个Java风格的C# UUID实现: https://gist.github.com/rickbeerendonk/13655dd24ec574954366
UUID可以像Java一样从最不重要和最重要的位创建,也可以公开它们。该实现具有显式转换为GUID和从GUID的隐式转换。
我不清楚方法;但是,可以通过以下方式将类型转换为GUID:
Guid iid = System.Runtime.InteropServices.Marshal.GenerateGuidForType(typeof(IFoo));
Guid.NewGuid()
吗? - SLaks