国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

C# XML 文檔注釋文件格式

2019-11-17 02:31:48
字體:
供稿:網(wǎng)友

C# xml 文檔注釋文件格式

在編寫 C# 代碼時,只要在注釋按照格式加入 XML 文檔注釋,例如:

/// <summary>/// 這里是類的注釋。/// </summary>public class MyClass { }

就可以通過設(shè)置項目的"屬性->生成->輸出->XML 文檔文件",來為當(dāng)前項目生成包含所有文檔注釋的 XML 文件。一般可用于 Visual Studio 的智能提示,或者利用Sandcastle等工具生成文檔。

下面,我會介紹生成的 XML 文件的格式和相關(guān)規(guī)則,都以 C# 編譯器生成的結(jié)果為基準(zhǔn)。

一、XML 文檔注釋文件格式

XML 文檔注釋的文件格式非常簡單,就是一個包含了所有注釋的列表,一個簡單的例子如下所示:

XML 文件的根節(jié)點是doc,下面包含兩個子節(jié)點assemblymembers。其中assembly是 XML 文件對應(yīng)的程序集名稱,members則包含了多個member節(jié)點,列出了所有的注釋(不區(qū)分是公共、受保護(hù)的還是私有成員)。member節(jié)點的name元素是一個唯一的標(biāo)識符,與程序集中定義的類、方法、屬性、字段等成員一一對應(yīng)。在編寫文檔注釋時指定的cref屬性,也會全部轉(zhuǎn)換為標(biāo)識符,而不是原先指定的成員名稱。

<?xml version="1.0"?><doc>    <assembly>        <name>Cyjb</name>    </assembly>    <members>        <member name="T:Cyjb.ArrayExt">            <summary>            提供數(shù)組的擴展方法。            </summary>        </member>        <member name="M:Cyjb.ArrayExt.Left``1(``0[],System.Int32)">            <summary>            從當(dāng)前數(shù)組的左端截取一部分。            </summary>            <typeparam name="T">數(shù)組中元素的類型。</typeparam>            <param name="array">從該數(shù)組返回其最左端截取的部分。</param>            <param name="length">要截取的元素個數(shù)。            如果為 <c>0</c>,則返回空數(shù)組。如果大于或等于 <paramref name="array"/> 的長度,            則返回整個數(shù)組的一個淺拷貝。</param>            <returns>從指定數(shù)組的左端截取的部分。</returns>            <exception cref="T:System.ArgumentNullException"><paramref name="array"/> 為 <c>null</c>。</exception>            <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception>        </member>        ...    </members></doc>

二、唯一標(biāo)識符規(guī)則

唯一標(biāo)識符總是Type:FullName的格式,其中Type表示對應(yīng)成員的類型,FullName是對應(yīng)成員的完全限定名,中間是用:分隔。

成員類型Type的可能值有:

  • N- 命名空間。
  • T- 類型,包括類、接口、結(jié)構(gòu)體、枚舉和委托。
  • F- 字段。
  • P- 屬性。
  • M- 方法,包括普通方法、構(gòu)造函數(shù)和運算符重載。
  • E- 事件。
  • !- 錯誤成員,一般是由于編譯器無法識別指定的成員類型,例如<see cref="MemberNotExists"/>,就會被編譯器轉(zhuǎn)換為<see cref="!:MemberNotExists"/>

完全限定名FullName則與成員本身的完全限定名類似,都是從命名空間的根開始,使用點分隔。不同的是:

  1. 成員名稱中的點會被替換為#,例如構(gòu)造函數(shù)的名稱.ctor會替換為#ctor
  2. 由關(guān)鍵字指定的類型,會被替換為相應(yīng)類型的完全限定名,例如object會替換為System.Objectvoid會替換為System.Void
  3. 指針類型會表示為*,引用類型會表示為@
  4. 多維數(shù)組會表示為[lowerbound:size,lowerbound:size],其中lowerbound是數(shù)組的指定維的下限,size是相應(yīng)的大小,未指定的話就直接省略。例如int[,]會替換為System.Int32[0:,0:]
  5. 泛型類型會省略掉泛型參數(shù),并在類名后添加`num,其中num是泛型參數(shù)的個數(shù)。例如SampleType<T, T2>會替換為SampleType`2
  6. 如果成員中出現(xiàn)了對類型的泛型參數(shù)的引用,會使用`idx代替,其中idx是相應(yīng)泛型參數(shù)在類型定義中的索引。例如上面的SampleType<T, T2>,對T的引用會替換為`0,對T2的引用會替換為`1
  7. 泛型方法同樣會省略掉泛型參數(shù),并在類名后添加``num,其中num是泛型參數(shù)的個數(shù)。例如SampleType<T, T2>.SampleMethod<T3>會替換為SampleType`2.SampleMethod``1
  8. 如果成員中出現(xiàn)了對方法的泛型參數(shù)的引用,會使用``idx代替,其中idx是相應(yīng)泛型參數(shù)在方法定義中的索引。例如上面的SampleType<T, T2>.SampleMethod<T3>,對T3的引用會替換為``0
  9. 泛型類型中的<>會被替換成{},例如IList<int>會替換為System.Collections.Generic.IList{System.Int32}
  10. 對于隱式和顯式類型轉(zhuǎn)換方法(op_Implicitop_Explicit),由于往往單憑參數(shù)類型不足以唯一區(qū)分方法,因此會在方法后額外添加~returnType,其中returnType是方法的返回值。例如Operator SampleType(int x)會替換為SampleType.op_Explicit(System.Int32)~SampleType

一個完整的實例如下所示,其中列出了每個成員對應(yīng)的唯一標(biāo)識符:

using System.Collections.Generic;// Identifier is N:Cyjbnamespace Cyjb{    /// <summary>    /// Identifier is T:Cyjb.SampleType    /// </summary>    public unsafe class SampleType    {        /// <summary>        /// Identifier is F:Cyjb.SampleType.SampleValue        /// </summary>        public const int SampleValue = 0;        /// <summary>        /// Identifier is F:Cyjb.SampleType.SampleValue2        /// </summary>        public int SampleValue2 = 0;        /// <summary>        /// Identifier is M:Cyjb.SampleType.#ctor        /// </summary>        public SampleType() { }        /// <summary>        /// Identifier is M:Cyjb.SampleType.#ctor(System.Int32)        /// </summary>        public SampleType(int value) { }        /// <summary>        /// Identifier is M:Cyjb.SampleType.SampleMethod        /// </summary>        public void SampleMethod() { }        /// <summary>        /// Identifier is M:Cyjb.SampleType.SampleMethod(System.Int32,System.Int32@,System.Int32*)        /// </summary>        public void SampleMethod(int a, ref int b, int* c) { }        /// <summary>        /// Identifier is M:Cyjb.SampleType.SampleMethod(System.Int32[],System.Int32[0:,0:],System.Int32[][])        /// </summary>        public void SampleMethod(int[] a, int[,] b, int[][] c) { }        /// <summary>        /// Identifier is M:Cyjb.SampleType.SampleMethod``1(``0,``0[],System.Collections.Generic.IList{``0},System.Collections.Generic.IList{System.Collections.Generic.IList{``0[]}})        /// </summary>        public void SampleMethod<T>(T a, T[] b, IList<T> c, IList<IList<T[]>> d) { }        /// <summary>        /// Identifier is M:Cyjb.SampleType.op_Addition(Cyjb.SampleType,Cyjb.SampleType)        /// </summary>        public static SampleType operator +(SampleType x, SampleType y) { return null; }        /// <summary>        /// Identifier is M:Cyjb.SampleType.op_Explicit(System.Int32)~Cyjb.SampleType        /// </summary>        public static explicit operator SampleType(int x) { return null; }        /// <summary>        /// Identifier is M:Cyjb.SampleType.op_Implicit(Cyjb.SampleType)~System.Int32        /// </summary>        public static implicit operator int(SampleType x) { return 0; }        /// <summary>        /// Identifier is P:Cyjb.SampleType.SamplePRoperty        /// </summary>        public int SampleProperty { get; set; }        /// <summary>        /// Identifier is P:Cyjb.SampleType.Item(System.Int32)        /// </summary>        public int this[int index] { get { return 0; } }        /// <summary>        /// Identifier is T:Cyjb.SampleType.SampleDelegate        /// </summary>        public delegate void SampleDelegate(int a);        /// <summary>        /// Identifier is E:Cyjb.SampleType.SampleEvent        /// </summary>        public event SampleDelegate SampleEvent;        /// <summary>        /// Identifier is T:Cyjb.SampleType.NestedType        /// </summary>        public class NestedType { }        /// <summary>        /// Identifier is T:Cyjb.SampleType.NestedType2`1        /// </summary>        public class NestedType2<T>        {            /// <summary>            /// Identifier is M:Cyjb.SampleType.NestedType2`1.TestMethod``1(`0,``0,System.Collections.Generic.IDictionary{`0,``0})            /// </summary>            public void TestMethod<T2>(T a, T2 b, IDictionary<T, T2> c) { }        }    }}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 常德市| 伽师县| 芜湖县| 保德县| 晴隆县| 佳木斯市| 册亨县| 庄浪县| 辉县市| 凤阳县| 淳化县| 子长县| 平原县| 泸州市| 弋阳县| 洪洞县| 巴林左旗| 伊吾县| 岳普湖县| 江津市| 汉源县| 桑植县| 手游| 江陵县| 阿鲁科尔沁旗| 抚州市| 龙陵县| 洪洞县| 满洲里市| 海丰县| 榆林市| 宁远县| 张北县| 松溪县| 肥乡县| 会理县| 江山市| 华池县| 韶山市| 孟村| 长丰县|