Guid Guid Guid Guid Struct

Definition

表示全局唯一标识符 (GUID)。 Represents a globally unique identifier (GUID).

public value class Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]
public struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
type Guid = struct
    interface IFormattable
Public Structure Guid
Implements IComparable, IComparable(Of Guid), IEquatable(Of Guid), IFormattable
Inheritance
Attributes
ComVisibleAttribute SerializableAttribute
Implements

Examples

下面的示例使用System.Runtime.InteropServices.GuidAttribute类,以将 GUID 分配给接口和一个用户定义的类。The following example uses the System.Runtime.InteropServices.GuidAttribute class to assign a GUID to an interface and to a user-defined class. 它通过调用检索的 GUID 值GetCustomAttribute方法,并将其与其他两个 Guid 来确定它们是否相等。It retrieves the value of the GUID by calling the GetCustomAttribute method, and compares it with two other GUIDs to determine whether they are equal.

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
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 使用。Note that the GuidAttribute attribute is typically used in an application to expose a type to COM. 如果编译此示例中,你可以运行程序集注册工具 (Regasm.exe)上生成的程序集来创建注册表 (.reg) 和类型库 (.tlb) 文件。If you compile this example, you can run the Assembly Registration tool (Regasm.exe) on the generated assembly to create registry (.reg) and type library (.tlb) files. .Reg 文件可用于 coclass 注册在注册表中,而.tlb 文件可以提供用于 COM 互操作的元数据。The .reg file can be used to register the coclass in the registry, and the .tlb file can provide metadata for COM interop.

Remarks

GUID 是一个 128 位整数 (16 个字节),它可用于跨所有计算机和网络中,任何唯一标识符是必需的。A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. 此类标识符具有重复的可能性非常小。Such an identifier has a very low probability of being duplicated.

Constructors

Guid(Byte[]) Guid(Byte[]) Guid(Byte[]) Guid(Byte[])

使用指定的字节数组初始化 Guid 类的新实例。 Initializes a new instance of the Guid structure by using the specified array of bytes.

Guid(ReadOnlySpan<Byte>) Guid(ReadOnlySpan<Byte>) Guid(ReadOnlySpan<Byte>) Guid(ReadOnlySpan<Byte>)
Guid(String) Guid(String) Guid(String) Guid(String)

使用指定字符串所表示的值初始化 Guid 类的新实例。 Initializes a new instance of the Guid structure by using the value represented by the specified string.

Guid(Int32, Int16, Int16, Byte[]) Guid(Int32, Int16, Int16, Byte[]) Guid(Int32, Int16, Int16, Byte[]) Guid(Int32, Int16, Int16, Byte[])

使用指定的整数和字节数组初始化 Guid 类的新实例。 Initializes a new instance of the Guid structure by using the specified integers and byte array.

Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

使用指定的整数和字节初始化 Guid 类的新实例。 Initializes a new instance of the Guid structure by using the specified integers and bytes.

Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

使用指定的无符号整数和字节初始化 Guid 类的新实例。 Initializes a new instance of the Guid structure by using the specified unsigned integers and bytes.

Fields

Empty Empty Empty Empty

Guid 结构的只读实例,其值均为零。 A read-only instance of the Guid structure whose value is all zeros.

Methods

CompareTo(Guid) CompareTo(Guid) CompareTo(Guid) CompareTo(Guid)

将此实例与指定 Guid 对象进行比较并返回它们的相对值。 Compares this instance to a specified Guid object and returns an indication of their relative values.

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

将此实例与指定对象进行比较并返回一个对二者的相对值的指示。 Compares this instance to a specified object and returns an indication of their relative values.

Equals(Guid) Equals(Guid) Equals(Guid) Equals(Guid)

返回一个值,该值指示此实例和指定的 Guid 对象是否表示相同的值。 Returns a value indicating whether this instance and a specified Guid object represent the same value.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。 Returns a value that indicates whether this instance is equal to a specified object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。 Returns the hash code for this instance.

NewGuid() NewGuid() NewGuid() NewGuid()

初始化 Guid 结构的新实例。 Initializes a new instance of the Guid structure.

Parse(String) Parse(String) Parse(String) Parse(String)

将 GUID 的字符串表示形式转换为等效的 Guid 结构。 Converts the string representation of a GUID to the equivalent Guid structure.

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>)
ParseExact(String, String) ParseExact(String, String) ParseExact(String, String) ParseExact(String, String)

将 GUID 的字符串表示形式转换为等效的 Guid 结构,前提是该字符串采用的是指定格式。 Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

ToByteArray() ToByteArray() ToByteArray() ToByteArray()

返回包含此实例的值的 16 元素字节数组。 Returns a 16-element byte array that contains the value of this instance.

ToString() ToString() ToString() ToString()

返回注册表格式的此实例值的字符串表示形式。 Returns a string representation of the value of this instance in registry format.

ToString(String) ToString(String) ToString(String) ToString(String)

根据所提供的格式说明符,返回此 Guid 实例值的字符串表示形式。 Returns a string representation of the value of this Guid instance, according to the provided format specifier.

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider)

根据所提供的格式说明符和区域性特定的格式信息,返回 Guid 类的此实例值的字符串表示形式。 Returns a string representation of the value of this instance of the Guid class, according to the provided format specifier and culture-specific format information.

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>)
TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid)
TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid)

将 GUID 的字符串表示形式转换为等效的 Guid 结构。 Converts the string representation of a GUID to the equivalent Guid structure.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)
TryParseExact(String, String, Guid) TryParseExact(String, String, Guid) TryParseExact(String, String, Guid) TryParseExact(String, String, Guid)

将 GUID 的字符串表示形式转换为等效的 Guid 结构,前提是该字符串采用的是指定格式。 Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

TryWriteBytes(Span<Byte>) TryWriteBytes(Span<Byte>) TryWriteBytes(Span<Byte>) TryWriteBytes(Span<Byte>)

Operators

Equality(Guid, Guid) Equality(Guid, Guid) Equality(Guid, Guid) Equality(Guid, Guid)

指示两个指定的 Guid 对象的值是否相等。 Indicates whether the values of two specified Guid objects are equal.

Inequality(Guid, Guid) Inequality(Guid, Guid) Inequality(Guid, Guid) Inequality(Guid, Guid)

指示两个指定的 Guid 对象的值是否不相等。 Indicates whether the values of two specified Guid objects are not equal.

Explicit Interface Implementations

IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object)
IFormattable.ToString(String, IFormatProvider) IFormattable.ToString(String, IFormatProvider) IFormattable.ToString(String, IFormatProvider) IFormattable.ToString(String, IFormatProvider)

Applies to