Guid 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示全局唯一标识符 (GUID)。
public value class Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
public value class Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, ISpanFormattable
public value class Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IParsable<Guid>, ISpanFormattable, ISpanParsable<Guid>
public value class Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IParsable<Guid>, ISpanFormattable, ISpanParsable<Guid>, IUtf8SpanFormattable
public value class Guid : IComparable, IFormattable
public struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
public readonly struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, ISpanFormattable
public readonly struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IParsable<Guid>, ISpanFormattable, ISpanParsable<Guid>
public readonly struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IParsable<Guid>, ISpanFormattable, ISpanParsable<Guid>, IUtf8SpanFormattable
[System.Serializable]
public struct Guid : IComparable, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
type Guid = struct
interface IFormattable
type Guid = struct
interface ISpanFormattable
interface IFormattable
type Guid = struct
interface IFormattable
interface IParsable<Guid>
interface ISpanFormattable
interface ISpanParsable<Guid>
type Guid = struct
interface IFormattable
interface IParsable<Guid>
interface ISpanFormattable
interface ISpanParsable<Guid>
interface IUtf8SpanFormattable
[<System.Serializable>]
type Guid = struct
interface IFormattable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Guid = struct
interface IFormattable
Public Structure Guid
Implements IComparable, IComparable(Of Guid), IEquatable(Of Guid), IFormattable
Public Structure Guid
Implements IComparable, IComparable(Of Guid), IEquatable(Of Guid), ISpanFormattable
Public Structure Guid
Implements IComparable, IComparable(Of Guid), IEquatable(Of Guid), IParsable(Of Guid), ISpanFormattable, ISpanParsable(Of Guid)
Public Structure Guid
Implements IComparable, IComparable(Of Guid), IEquatable(Of Guid), IParsable(Of Guid), ISpanFormattable, ISpanParsable(Of Guid), IUtf8SpanFormattable
Public Structure Guid
Implements IComparable, IFormattable
- 继承
- 属性
- 实现
示例
以下示例使用 System.Runtime.InteropServices.GuidAttribute 类将 GUID 分配给接口和用户定义的类。 它通过调用 GetCustomAttribute 方法检索 GUID 的值,并将其与其他两个 GUID 进行比较以确定它们是否相等。
using namespace System;
using namespace System::Runtime::InteropServices;
// Guid for the interface IMyInterface.
[Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")]
public interface class IMyInterface
{
public:
void MyMethod();
};
// Guid for the coclass MyTestClass.
[Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
public ref class MyTestClass: public IMyInterface
{
public:
virtual void MyMethod(){}
};
int main()
{
Attribute^ IMyInterfaceAttribute = Attribute::GetCustomAttribute( IMyInterface::typeid, GuidAttribute::typeid );
// The Value property of GuidAttribute returns a string.
System::Console::WriteLine( String::Concat( "IMyInterface Attribute: ", (dynamic_cast<GuidAttribute^>(IMyInterfaceAttribute))->Value ) );
// Using the string to create a guid.
Guid myGuid1 = Guid(dynamic_cast<GuidAttribute^>(IMyInterfaceAttribute)->Value);
// Using a byte array to create a guid.
Guid myGuid2 = Guid(myGuid1.ToByteArray());
// Equals is overridden to perform a value comparison.
if ( myGuid1.Equals( myGuid2 ) )
System::Console::WriteLine( "myGuid1 equals myGuid2" );
else
System::Console::WriteLine( "myGuid1 not equals myGuid2" );
// Equality operator can also be used to determine if two guids have same value.
if ( myGuid1 == myGuid2 )
System::Console::WriteLine( "myGuid1 == myGuid2" );
else
System::Console::WriteLine( "myGuid1 != myGuid2" );
}
// The example displays the following output:
// IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
// myGuid1 equals myGuid2
// myGuid1 == myGuid2
using System;
using System.Runtime.InteropServices;
// Guid for the interface IMyInterface.
[Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")]
interface IMyInterface
{
void MyMethod();
}
// Guid for the coclass MyTestClass.
[Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
public class MyTestClass : IMyInterface
{
public void MyMethod() {}
public static void Main( string []args )
{
GuidAttribute IMyInterfaceAttribute = (GuidAttribute) Attribute.GetCustomAttribute(typeof(IMyInterface), typeof(GuidAttribute));
System.Console.WriteLine("IMyInterface Attribute: " + IMyInterfaceAttribute.Value );
// Use the string to create a guid.
Guid myGuid1 = new Guid(IMyInterfaceAttribute.Value );
// Use a byte array to create a guid.
Guid myGuid2 = new Guid(myGuid1.ToByteArray());
if (myGuid1.Equals(myGuid2))
System.Console.WriteLine("myGuid1 equals myGuid2");
else
System.Console.WriteLine("myGuid1 does not equal myGuid2" );
// Equality operator can also be used to determine if two guids have same value.
if ( myGuid1 == myGuid2 )
System.Console.WriteLine( "myGuid1 == myGuid2" );
else
System.Console.WriteLine( "myGuid1 != myGuid2" );
}
}
// The example displays the following output:
// IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
// myGuid1 equals myGuid2
// myGuid1 == myGuid2
open System
open System.Runtime.InteropServices
// Guid for the interface IMyInterface.
[<Guid "F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4">]
type IMyInterface =
abstract MyMethod: unit -> unit
// Guid for the coclass MyTestClass.
[<Guid "936DA01F-9ABD-4d9d-80C7-02AF85C822A8">]
type MyTestClass() =
interface IMyInterface with
member _.MyMethod() = ()
let IMyInterfaceAttribute =
Attribute.GetCustomAttribute(typeof<IMyInterface>, typeof<GuidAttribute>) :?> GuidAttribute
printfn $"IMyInterface Attribute: {IMyInterfaceAttribute.Value}"
// Use the string to create a guid.
let myGuid1 = Guid IMyInterfaceAttribute.Value
// Use a byte array to create a guid.
let myGuid2 = Guid(myGuid1.ToByteArray())
if myGuid1.Equals myGuid2 then
printfn "myGuid1 equals myGuid2"
else
printfn "myGuid1 does not equal myGuid2"
// Equality operator can also be used to determine if two guids have same value.
if myGuid1 = myGuid2 then
printfn "myGuid1 == myGuid2"
else
printfn "myGuid1 <> myGuid2"
// The example displays the following output:
// IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
// myGuid1 equals myGuid2
// myGuid1 == myGuid2
Imports System.Runtime.InteropServices
' Guid for the interface IMyInterface.
<Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")> _
Interface IMyInterface
Sub MyMethod()
End Interface
' Guid for the coclass MyTestClass.
<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")> _
Public Class MyTestClass
Implements IMyInterface
Public Sub MyMethod() Implements IMyInterface.MyMethod
End Sub
Public Shared Sub Main()
Dim IMyInterfaceAttribute As GuidAttribute = CType(Attribute.GetCustomAttribute(GetType(IMyInterface), GetType(GuidAttribute)),
GuidAttribute)
Console.WriteLine("IMyInterface Attribute: " + IMyInterfaceAttribute.Value)
' Use the string to create a guid.
Dim myGuid1 As New Guid(IMyInterfaceAttribute.Value)
' Use a byte array to create a guid.
Dim myGuid2 As New Guid(myGuid1.ToByteArray())
If myGuid1.Equals(myGuid2) Then
Console.WriteLine("myGuid1 equals myGuid2")
Else
Console.WriteLine("myGuid1 does not equal myGuid2")
End If
' The equality operator can also be used to determine if two guids have same value.
If myGuid1.ToString() = myGuid2.ToString() Then
Console.WriteLine("myGuid1 == myGuid2")
Else
Console.WriteLine("myGuid1 != myGuid2")
End If
End Sub
End Class
' The example displays the following output:
' IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
' myGuid1 equals myGuid2
' myGuid1 == myGuid2
请注意, GuidAttribute 特性通常在应用程序中用于向 COM 公开类型。 如果编译此示例,则可以在生成的 程序集上运行程序集注册工具 (Regasm.exe) ,以创建注册表 (.reg) 和类型库 (.tlb) 文件。 .reg 文件可用于在注册表中注册 coclass,.tlb 文件可以为 COM 互操作提供元数据。
注解
GUID 是一个 128 位整数 (16 字节) ,可在需要唯一标识符的所有计算机和网络中使用。 此类标识符重复的可能性非常低。
构造函数
Guid(Byte[]) |
使用指定的字节数组初始化 Guid 类的新实例。 |
Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) |
使用指定的整数和字节初始化 Guid 类的新实例。 |
Guid(Int32, Int16, Int16, Byte[]) |
使用指定的整数和字节数组初始化 Guid 类的新实例。 |
Guid(ReadOnlySpan<Byte>) |
通过使用指定的只读字节范围所表示的值来初始化 Guid 结构的新实例。 |
Guid(ReadOnlySpan<Byte>, Boolean) |
表示全局唯一标识符 (GUID)。 |
Guid(String) |
使用指定字符串所表示的值初始化 Guid 类的新实例。 |
Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) |
使用指定的无符号整数和字节初始化 Guid 类的新实例。 |
字段
Empty |
Guid 结构的只读实例,其值均为零。 |
方法
运算符
Equality(Guid, Guid) |
指示两个指定的 Guid 对象的值是否相等。 |
GreaterThan(Guid, Guid) |
比较两个值以确定哪个值更大。 |
GreaterThanOrEqual(Guid, Guid) |
比较两个值以确定哪个值更大或相等。 |
Inequality(Guid, Guid) |
指示两个指定的 Guid 对象的值是否不相等。 |
LessThan(Guid, Guid) |
比较两个值以确定哪个值较小。 |
LessThanOrEqual(Guid, Guid) |
比较两个值以确定哪个值小于或等于。 |
显式接口实现
IComparable.CompareTo(Object) |
将此实例与指定 Guid 对象进行比较并返回它们的相对值。 |
IFormattable.ToString(String, IFormatProvider) |
根据所提供的格式说明符和区域性特定的格式信息,返回此实例值的字符串表示形式。 |
ISpanFormattable.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
尝试将当前实例的值格式化为提供的字符范围。 |
IUtf8SpanFormattable.TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
尝试将当前实例的值格式化为 UTF-8,将其设置为提供的字节范围。 |