老外编的程序(五)--如何使用全球标识符(GUID)

80酷酷网    80kuku.com

  guid|程序// Snippet shows how interfaces and coclasses can adorn the Guid attribute.
// Running the regasm will generate .reg and .tlb files. Reg file can be
// used to register the interface and coclass with the registry. Tlb file
// are used to do interop. Also illustrates how System.Guid can be constructed
// and how guid objects can be compared.

namespace GuidSnippet
{
    //<Snippet1>
    using System;
    using System.Runtime.InteropServices;

    // Guid for the interface IFoo.
    [Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")]
    interface IFoo
    {
        void Foo();
    }

    // Guid for the coclass CFoo.
    [Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
    public class CFoo : IFoo
    {
        // 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 Foo() {}

        public static void Main( string []args )
        {
            // Snippet 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.

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

            Attribute IFooAttribute = Attribute.GetCustomAttribute( typeof( IFoo ), typeof( GuidAttribute ) );
            
            // The Value property of GuidAttribute returns a string.
            System.Console.WriteLine( "IFoo Attribute: " + ((GuidAttribute)IFooAttribute).Value );    

            // Using the string to create a guid.
            Guid guidFoo1 = new Guid( ((GuidAttribute)IFooAttribute).Value );
            // Using a byte array to create a guid.
            Guid guidFoo2 = new Guid ( guidFoo1.ToByteArray() );

            // Equals is overriden and so value comparison is done though references are different.
            if ( guidFoo1.Equals( guidFoo2 ) )
                System.Console.WriteLine( "guidFoo1 equals guidFoo2" );
            else
                System.Console.WriteLine( "guidFoo1 not equals guidFoo2" );

            // Equality operator can also be used to determine if two guids have same value.
            if ( guidFoo1 == guidFoo2 )
                System.Console.WriteLine( "guidFoo1 == guidFoo2" );
            else
                System.Console.WriteLine( "guidFoo1 != guidFoo2" );
        
            // CompareTo returns 0 if the guids have same value.
            if ( guidFoo1.CompareTo( guidFoo2 ) == 0 )
                System.Console.WriteLine( "guidFoo1 compares to guidFoo2" );
            else
                System.Console.WriteLine( "guidFoo1 does not compare to guidFoo2" );

            System.Console.ReadLine();

            //Output.
            //IFoo Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
            //guidFoo1 equals guidFoo2
            //guidFoo1 == guidFoo2
            //guidFoo1 compares to guidFoo2
        }
    }
}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: