【问题标题】:C# displaying documentation on method when dot operator is invoked on a class [duplicate]C#在类上调用点运算符时显示方法文档[重复]
【发布时间】:2018-10-19 14:49:53
【问题描述】:

有没有一种方法可以以编程方式记录类中的方法并显示文档,就像用户调用点运算符时一样(例如,String.[method]),并且可以查看类中特定方法的作用?见屏幕截图。我想在 C# 中为一个类创建自定义方法,然后记录它。然后当用户使用该类并启动点运算符 (.) 时,他们将看到方法和描述该方法的文档

【问题讨论】:

  • 在方法之前使用///

标签: c#


【解决方案1】:

是的。使用XML documentation comments

在 Visual C# 中,您可以通过包括 特殊注释字段中的 XML 元素(用三斜杠表示) 在源代码中直接在代码块之前 cmets 指,例如:

/// <summary>  
///  This class performs an important function.  
/// </summary>  
public class MyClass{}  

另见Documenting Your Code With XML Comments

&lt;summary&gt; 标签非常重要,我们建议您包含 因为它的内容是类型或成员的主要来源 IntelliSense 或 API 参考文档中的信息。

【讨论】:

    【解决方案2】:

    您可以为成员使用内联文档 xml 标签。使用摘要来解释方法或其他成员。您可以使用其他标签来获取详细的文档。

    MSDN Documentation

    /// <summary>
    /// The main Math class.
    /// Contains all methods for performing basic math functions.
    /// </summary>
    public class Math
    {
        // Adds two integers and returns the result
        /// <summary>
        /// Adds two integers and returns the result.
        /// </summary>
        public static int Add(int a, int b)
        {
            // If any parameter is equal to the max value of an integer
            // and the other is greater than zero
            if ((a == int.MaxValue && b > 0) || (b == int.MaxValue && a > 0))
                throw new System.OverflowException();
    
            return a + b;
        }
    }
    

    您可以使用一些第三方工具来使用此标签创建 html 或 chm 文档文件。

    一个例子是Sandcastle documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多