GUID概念



GUID概念

 

 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。

GUID 主要用于在拥有多个节点、多台计算机的网络或系统中。分配必须具有唯一性的标识符。

在 Windows 平台上。GUID 广泛应用于微软的产品中,用于标识如如注冊表项、类及接口标识、数据库、系统文件夹等对象

 

GUID格式

 GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,当中每一个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。比如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。

 

 

GUID特点

★GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。 ★世界上的不论什么两台计算机都不会生成反复的 GUID 值。

★须要GUID的时候,能够全然由算法自己主动生成。不须要一个权威机构来管理。 ★GUID的长度固定,而且相对而言较短小。很适合于排序、标识和存储。

 

 

GUID生成算法

网上流传。用到了以太网卡地址、纳秒级时间、芯片ID码和很多可能的数字。有待验证。

 

 

GUID样例

1.注冊表,GUIDs被用于整个Windows环境。

当你在一个Windows系统中细致阅读注冊表时,你能够看到GUIDs被广泛用于唯一识别程序。

特别地。它们作为程序的Ids集中在HKEY_CLASSES_ROOT部分(AppID键)。

 

2.C#。Sqlserver的唯一表示。当基于web的应用程序。并发量非常大的情况下。并发訪问上千以上时,在数据中,使用 int id identiy(1,1),出现同样的概率会大,GUID,就有所改善。

class Program
    {
        // 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
        {
            // Run regasm on this assembly to create .reg and .tlb files.
            // Reg file can be used to register this coclass in the registry.
            // Tlb file will be used to do interop.


            public void MyMethod() { }


            public static void Main(string[] args)
            {
                // Example addresses the following in System.Runtime.InterOpServices.GuidAttribute.
                // How to specify the attribute on interface/coclass.
                // Retrieve the GuidAttribute from an interface/coclass.
                // Value property on GuidAttribute class.


                // Example addresses the following in System.Guid.
                // Constructor Guid(string).
                // Constructor Guid(ByteArray).
                // Equals.
                // Operator ==.
                // CompareTo.


                Attribute IMyInterfaceAttribute = Attribute.GetCustomAttribute(typeof(IMyInterface), typeof(GuidAttribute));


                // The Value property of GuidAttribute returns a string. 
                System.Console.WriteLine("IMyInterface Attribute: " + ((GuidAttribute)IMyInterfaceAttribute).Value);


                // Using the string to create a guid.
                Guid myGuid1 = new Guid(((GuidAttribute)IMyInterfaceAttribute).Value);
                // Using a byte array to create a guid.
                Guid myGuid2 = new Guid(myGuid1.ToByteArray());


                // Equals is overridden and so value comparison is done though references are different.
                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");


                // CompareTo returns 0 if the guids have same value.
                if (myGuid1.CompareTo(myGuid2) == 0)
                    System.Console.WriteLine("myGuid1 compares to myGuid2");
                else
                    System.Console.WriteLine("myGuid1 does not compare to myGuid2");


                System.Console.ReadLine();


                //Output.
                //IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
                //myGuid1 equals myGuid2
                //myGuid1 == myGuid2
                //myGuid1 compares to myGuid2

                //GENERATE GUIDS
                //for (int i = 0; i <100; i++)
                //{
                //    Console.WriteLine(System.Guid.NewGuid().ToString());
                //}
              
                //Console.ReadLine();
               


            }


        }


}

posted @ 2017-05-07 20:47  yfceshi  阅读(642)  评论(0编辑  收藏  举报