在C#中继承构造函数文档?

时间:2011-12-20 21:13:52

标签: c# exception inheritance

我在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) {}
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

/// <inheritdoc />

似乎可以在Visual Studio 16.4中做到这一点,即

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

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