【问题标题】:Inherit constructor documentation in C#?在 C# 中继承构造函数文档?
【发布时间】:2012-01-24 19:23:21
【问题描述】:

我在 c# 中有一个自定义异常类:

public class InformationException : Exception {}

现在这当然行不通了,因为异常没有构造函数。

只是确保:我真的必须自己添加它们? :

public class InformationException : Exception
{
    public InformationException() : base() {}
    public InformationException(string message): base(message) {}
    public InformationException(string message, Exception innerException) : base(message, innerException) {}
}

但是为了让课程真正有用,我必须添加文档:

public class InformationException : Exception
{
    /// <summary>
    /// Initializes a new instance of the System.Exception class.
    /// </summary>
    public InformationException() : base() {}

    /// <summary>
    /// Initializes a new instance of the System.Exception class with a specified error message.
    /// </summary>
    /// <param name="message"> The message that describes the error.</param>
    public InformationException(string message): base(message) {}

    /// <summary>
    /// Initializes a new instance of the System.Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.
    /// </summary>
    /// <param name="message"> The error message that explains the reason for the exception.</param>
    /// <param name="innerException">The exception that is the cause of the current exception, or a null reference 
    /// (Nothing in Visual Basic) if no inner exception is specified.</param>
    public InformationException(string message, Exception innerException) : base(message, innerException) {}
}

我现在必须重复:

public class ClientException : InformationException { }

public class BusinessRuleException : InformationException { }

public class InvisibleException : Exception { }

public class ProgrammerException : InvisibleException { }

C#in the last 3 years 中没有我错过的任何更改?这仍然是继承类的预期方式吗?


更新:哎呀,原来你还必须提供受保护的构造函数:

public class InformationException : Exception
{
    public InformationException() : base() {}
    public InformationException(string message): base(message) {}

    /// <summary>
    /// Initializes a new instance of the System.Exception class with serialized data.
    /// </summary>
    /// <param name="info">The System.Runtime.Serialization.SerializationInfo that holds the serialized
    /// object data about the exception being thrown.</param>
    /// <param name="context">The System.Runtime.Serialization.StreamingContext that contains contextual
    /// information about the source or destination.</param>
    /// <exception cref="System.ArgumentNullException">The info parameter is null</exception>
    /// <exception cref="System.Runtime.Serialization.SerializationException">The class name is null or System.Exception.HResult is zero (0).</exception>
    protected InformationException(SerializationInfo info, StreamingContext context);

    protected Exception(SerializationInfo info, StreamingContext context): base(info, context) {}
    public InformationException(string message, Exception innerException) : base(message, innerException) {}
}

【问题讨论】:

  • 定义“这当然行不通”的意思,因为它当然行得通。它是否有用是另一回事。
  • @ChrisWue 是的,我想知道自 2009 年以来 C# 语言是否有任何发展。或者更具体地说,为了解决我的问题,是否有某种 &lt;summary base&gt;&lt;summary&gt; 语法
  • Alex Yakunin 写道:“我有一个更好的答案:FiXml。”。不确定它是否能解决您的具体问题,但它在概念上肯定比在源代码中复制 cmets 更干净。

标签: c# exception inheritance


【解决方案1】:
/// <inheritdoc />

似乎在 Visual Studio 16.4 中可以解决问题,即

/// <inheritdoc />
public ClientException(string message): base(message) {}

当然,这仅适用于与基本构造函数参数同名的参数。

【讨论】:

    【解决方案2】:

    this answer 中所述,您可以使用GhostDoc

    【讨论】:

    • 我同意,但如果那是 OP 想要的,那么那里有一些东西。
    • @aqwet 如果一个人想抛出异常,他们将需要一种方法来创建它......(?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2017-08-09
    • 2019-01-21
    • 1970-01-01
    • 2012-11-14
    • 2018-06-28
    相关资源
    最近更新 更多