新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:老外编的程序(五)--如何使用全球标识符(GUID)

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

新客网 XKER.COM 2003-07-12 来源: 收藏本文
// 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
        }
    }
}
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐