【问题标题】:Documenting an exception in C# code在 C# 代码中记录异常
【发布时间】:2011-08-28 02:54:58
【问题描述】:

我在mscorlib.xml(从摘要生成的 XML 文件)中注意到它包含:

<member name="M:System.Console.ReadKey">
  <summary>Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.</summary>
  <returns>A <see cref="T:System.ConsoleKeyInfo" /> object that describes the <see cref="T:System.ConsoleKey" /> constant and Unicode character, if any, that correspond to the pressed console key. The <see cref="T:System.ConsoleKeyInfo" /> object also describes, in a bitwise combination of <see cref="T:System.ConsoleModifiers" /> values, whether one or more SHIFT, ALT, or CTRL modifier keys was pressed simultaneously with the console key.</returns>
  <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Console.In" /> property is redirected from some stream other than the console.</exception>
  <filterpriority>1</filterpriority>
</member>

从我特别感兴趣的那个例子中:

<exception cref="T:System.InvalidOperationException">The <see cref="P:System.Console.In" /> property is redirected from some stream other than the console.</exception>

您如何在代码中记录异常以便它们最终出现在 XML 中?

【问题讨论】:

  • 奇怪的问题,因为你自己已经给出了答案:-)

标签: c# exception documentation


【解决方案1】:

这是一个例子:

/// <exception cref="ArgumentException">
/// Thrown when the <typeparamref name="TConcrete"/> is a type
/// that can not be created by the container.
/// </exception>

换句话说,与您在问题中已经显示的代码 sn-p 完全相同。

【讨论】:

【解决方案2】:

您可以在源代码的 XML cmets 中添加带有 &lt;exception&gt; 标记的异常文档。例如:

///<summary>Summary of method/property/etc</summary>
///<returns>Returns a value</returns>
///<exception>Throws an exception</exception>

【讨论】:

    猜你喜欢
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2010-09-08
    相关资源
    最近更新 更多