c#如何使用 XML 文档功能
创始人
2024-11-16 15:02:08
0次

示例

// If compiling from the command line, compile with: -doc:YourFileName.xml

/// 
/// Class level summary documentation goes here.
/// 
/// 
/// Longer comments can be associated with a type or member through
/// the remarks tag.
/// 
public class TestClass : TestInterface
{
  /// 
  /// Store for the Name property.
  /// 
  private string _name = null;

  /// 
  /// The class constructor.
  /// 
  public TestClass()
  {
    // TODO: Add Constructor Logic here.
  }

  /// 
  /// Name property.
  /// 
  /// 
  /// A value tag is used to describe the property value.
  /// 
  public string Name
  {
    get
    {
      if (_name == null)
      {
        throw new System.Exception("Name is null");
      }
      return _name;
    }
  }

  /// 
  /// Description for SomeMethod.
  /// 
  ///  Parameter description for s goes here.
  /// 
  /// You can use the cref attribute on any tag to reference a type or member
  /// and the compiler will check that the reference exists.
  /// 
  public void SomeMethod(string s)
  {
  }

  /// 
  /// Some other method.
  /// 
  /// 
  /// Return values are described through the returns tag.
  /// 
  /// 
  /// Notice the use of the cref attribute to reference a specific method.
  /// 
  public int SomeOtherMethod()
  {
    return 0;
  }

  public int InterfaceMethod(int n)
  {
    return n * n;
  }

  /// 
  /// The entry point for the application.
  /// 
  ///  A list of command line arguments.
  static int Main(System.String[] args)
  {
    // TODO: Add code to start application here.
    return 0;
  }
}

/// 
/// Documentation that describes the interface goes here.
/// 
/// 
/// Details about the interface go here.
/// 
interface TestInterface
{
  /// 
  /// Documentation that describes the method goes here.
  /// 
  /// 
  /// Parameter n requires an integer argument.
  /// 
  /// 
  /// The method returns an integer.
  /// 
  int InterfaceMethod(int n);
}

该示例生成一个包含以下内容的 .xml 文件。

<?xml version="1.0"?>

  
    xmlsample
  
  
    
      
      Class level summary documentation goes here.
      
      
      Longer comments can be associated with a type or member through
      the remarks tag.
      
    
    
      
      Store for the Name property.
      
    
    
      
      The class constructor.
      
    
    
      
      Name property.
      
      
      A value tag is used to describe the property value.
      
    
    
      
      Description for SomeMethod.
      
       Parameter description for s goes here.
      
      You can use the cref attribute on any tag to reference a type or member
      and the compiler will check that the reference exists.
      
    
    
      
      Some other method.
      
      
      Return values are described through the returns tag.
      
      
      Notice the use of the cref attribute to reference a specific method.
      
    
    
      
      The entry point for the application.
      
       A list of command line arguments.
    
    
      
      Documentation that describes the interface goes here.
      
      
      Details about the interface go here.
      
    
    
      
      Documentation that describes the method goes here.
      
      
      Parameter n requires an integer argument.
      
      
      The method returns an integer.
      
    
  

编译代码

若要编译该示例,请输入以下命令:

csc XMLsample.cs /doc:XMLsample.xml

此命令创建 XML 文件 XMLsample.xml,可在浏览器中或使用 TYPE 命令查看该文件。

可靠编程

XML 文档以 /// 开头。 创建新项目时,向导会放置一些以 /// 开头的行。 处理这些注释时存在一些限制:

1.文档必须是格式正确的 XML。 如果 XML 格式不正确,则会生成警告,并且文档文件将包含一条注释,指出遇到错误。

2.开发人员可以随意创建自己的标记集。 有一组推荐的标记。 部分建议标记具有特殊含义:

  • 标记用于描述参数。 如果已使用,编译器会验证该参数是否存在,以及文档是否描述了所有参数。 如果验证失败,编译器会发出警告。
  • cref 属性可以附加到任何标记,以引用代码元素。 编译器验证此代码元素是否存在。 如果验证失败,编译器会发出警告。 编译器在查找 cref 属性中描述的类型时会考虑所有 using 语句。
  • 标记由 Visual Studio 中的 IntelliSense 用于显示有关某个类型或成员的附加信息。
    备注

XML 文件不提供有关该类型和成员的完整信息(例如,它不包含任何类型信息)。 若要获取有关类型或成员的完整信息,请将文档文件与对实际类型或成员的反射一起使用。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...