简体   繁体   English

从抽象类派生的Force类来实现构造函数

[英]Force class derived from abstract class to implement constructor

I have a custom exception which looks like the following: 我有一个自定义异常,如下所示:

public abstract class MyException : Exception
{
    public MyException()
    {
    }

    public MyException(string message) : base(message)
    {
    }

    public MyException(string message, Exception inner) : base(message, inner)
    {

    }

    public abstract HttpStatusCode GetHttpStatusCode();
}

And a derived class: 派生类:

public class ForbiddenException : MyException
{
    public override HttpStatusCode GetHttpStatusCode()
    {
        return HttpStatusCode.Forbidden;
    }
}

I want the derived classes to either use, or be forced to implement, the constructor formats in the abstract class. 我希望派生类要么使用,要么被迫实现抽象类中的构造函数格式。

Currently using the above, when I try to create a ForbiddenException , I get the following error: 目前使用上面的内容,当我尝试创建ForbiddenException ,我收到以下错误:

throw new ForbiddenException("Request forbidden");

ForbiddenException does not contain a constructor that takes 1 arguments

I could manually put the constructors into each derived class of MyException but this is error prone + repeating myself. 我可以手动将构造函数放入MyException每个派生类中,但这很容易出错并重复自己。

Any advice would be appreciated. 任何意见,将不胜感激。

If you want to avoid copying & pasting/errors when re-writing, consider using a Text Template. 如果要在重写时避免复制和粘贴/错误,请考虑使用文本模板。 I've placed this in a .tt template in my toy class library, which I also copied your MyException into: 我把它放在我的玩具类库中的.tt模板中,我还将你的MyException复制到:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
using System;

namespace ClassLibrary2
{

<# StandardConstructors("public","ForbiddenException","MyException"); #>

}
<#+
  public void StandardConstructors(string modifiers,
       string derivedException, string baseException)
    {
        Write(modifiers);
        Write(" partial class ");
        Write(derivedException);
        Write(":");
        WriteLine(baseException);
        WriteLine("{");

        Write("\tpublic ");
        Write(derivedException);
        WriteLine("() {}");

        Write("\tpublic ");
        Write(derivedException);
        WriteLine("(string message):base(message)");
        WriteLine("\t{}");

        Write("\tpublic ");
        Write(derivedException);
        WriteLine("(string message, Exception inner):base(message,inner)");
        WriteLine("\t{}");

        WriteLine("}");
    }
#>

The only error I immediately get is I've not implemented GetHttpStatusCode but I'd expect you to be writing that in a separate file that also defines the same partial ForbiddenException class. 我立即得到的唯一错误是我没有实现GetHttpStatusCode但我希望你在一个单独的文件中编写它,该文件也定义了相同的部分ForbiddenException类。 Each new derived exception just needs you to add another StandardConstructors line to the template file. 每个新派生的异常只需要您将另一个StandardConstructors行添加到模板文件中。 Far less error prone. 更不容易出错。

Of course, you can make this as fancy as you like, I just wrote this from scratch in a few minutes 1 . 当然,你可以随心所欲地做到这一点,我只是在几分钟内从头开始写这个1

This is one the above template produces: 这是以上模板产生的一个:

using System;

namespace ClassLibrary2
{

public partial class ForbiddenException:MyException
{
    public ForbiddenException() {}
    public ForbiddenException(string message):base(message)
    {}
    public ForbiddenException(string message, Exception inner):base(message,inner)
    {}
}

}

Not pretty but it doesn't have to be - it's generated code. 不漂亮,但它不一定是 - 它是生成的代码。


1 Eg if the tabs/spacing really irks you, but also if eg you want to have more control over modifiers, etc. 1例如,如果标签/间距确实让您感到烦恼,那么例如您是否希望更多地控制修饰符等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强制派生 class 以实现带有参数的基本 class 构造函数 - Force derived class to implement base class constructor with parameter(s) 有没有一种方法可以强制派生类实现抽象类或嵌套在基类中的接口? - Is there a way to force a derived class to implement an abstract class or an interface nested in base class? 强制派生类实现接口 - Force derived class to implement interface 如何在非抽象基类的派生类中强制重写? - How to force overrides in a derived class from non abstract base classes? 模拟从抽象类派生的抽象类 - Mocking an abstract class derived from an abstract class 带构造函数的抽象类,强制继承类来调用它 - Abstract class with constructor, force inherited class to call it 具有一个以上参数化构造函数的抽象类和具有参数化构造函数的派生类 - abstract class with more then one parameterized constructor and derived class with parameterized constructor 从抽象类派生的用户控件 - Usercontrol derived from abstract class 努力在派生类中实现抽象属性 - Struggling to implement abstract property in derived class 派生自另一个类且派生自抽象类的类的要包含在参数化构造函数中的c#字段 - c# Fields to include in parameterized constructor, of a class that has derived from another class that has derived from abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM