chao's profile清凉水都PhotosBlogListsMore Tools Help

Blog


    September 17

    .Net(C#)结合IntelliSense写文档注释

    del.icio.us标签: , ,

    作者:e-bread   

    在使用VisualStudio的时候,大家可能对内置的IntelliSense功能赞叹不已。高高兴兴的自己写个类,发现这个所谓的IntelliSense并不买账,怎样才能实现.Net系统库那样的IntelliSense功能呢?其实.Net内置了一个XML文档注释功能,VisualStudio可以根据生成的XML文档注释内容,通过反射机制,从而实现IntelliSense功能。当然如果你对反射足够了解,也可以自己分析生成的XML文档,不过这部分不是我们这次关注的问题。

    打开MSDN,搜索XML文档注释,你会发现.Net提供了大量的文档注释标记,实际上多数标签对于VisualStudio的IntelliSense来说是没有用的,能用上的也就那么几个,其余的是做其它用处的,例如类型检测等,由于是初步,也就不多说了。啰嗦了半天,下面进入正题,能被IntelliSense识别的标签是:<summary>,<param>,<exception>,换句话说只有这三个标签的内容会显示在快捷信息中。下面我们看一个例子。

           /// <summary>
          /// 这是摘要信息
          /// </summary>
          /// <param name="i">参数1信息</param>
          /// <param name="fl">参数2信息</param>
          /// <exception cref="System.Exception">异常信息,在Cref中制定错误类型</exception>【!注意这里是一行,由于页面宽度换行了。】
           public string SampleMethod(int i, bool fl)
           {   return " " ;}

    细心的人应当发现了,这里注释行前用了三个"/"而不是常用的两个"/",这是XML文档注释的特征,说明这些注释是要编译的,否则不编译。当然类似块注释,XML注释也可以用"/**  */"进行块注释。不过和一般的块注释不同,XML 的注释块中间的每行前都要加一个"*",看起来是这个样子:

        /**
         * <summary>摘要信息</summary>
         */

    个人感觉还不如单行方便,而且VisualStudio在块的情况下的IntelliSense支持不好,不会自动添加结束标签,个人不建议使用这种注释方式。

    由于是XML文档注释,因此除了前面的“///”外,格式是符合XML规范的,<summary>标签显示对应的摘要信息,<exception>则根据"cref"引用的类型显示异常信息,这两个标签会在IntelliSense成员列表的快速信息中显示,以上面的例子显示如下:

    string SampleMethod(int i, bool fl)
           这是摘要信息
    Exceptions:
          System.Exception

    对于<param>标签,则根据"cref"引用的参数名,根据你在键入的对应函数显示对应的提示信息,例如你在上例中输入参数fl,则快捷信息显示为:

    string SampleMethod(int i, bool fl)
     fl:参数2注释

    我们可以发现异常信息并没有显示出来,并不是VisualStudio没有识别,只不过没有放在快捷信息中,打开对象浏览器(object  browser)这些信息就出来了,其实想<remark>,<returns>也会显示在对象浏览器中,不会显示在快捷信息中。因此我们可以将备注信息和返回值的详细信息写在这两个标签中,当需要查看是可以到对象浏览器中仔细查看。

    只要遵守XML注释规范,那么你的代码也可以智能感知,花一点时间写这些注释,会大大提高后面的工作效率,多人合作更是如此,还不赶快去试试!

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://ebakeshop.spaces.live.com/blog/cns!1A66D6C80502E805!325.trak
    Weblogs that reference this entry
    • None