本文整理汇总了C#中System.Guid结构体的典型用法代码示例。如果您正苦于以下问题:C# Guid结构体的具体用法?C# Guid怎么用?C# Guid使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
Guid结构体属于System命名空间,在下文中一共展示了Guid结构体的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MyMethod
//引入命名空间
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" );
}
}
输出:
IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4 myGuid1 equals myGuid2 myGuid1 == myGuid2
示例2: new Guid
//引入命名空间
using System;
public class MainClass {
public static void Main() {
Guid g1 = new Guid("{0d57629c-7d6e-4847-97cb-9e2fc25083fe}");
Guid g2 = new Guid("0d57629c7d6e484797cb9e2fc25083fe");
Console.WriteLine(g1 == g2); // True
}
}