继承一个没有任何构造函数的抽象类 [英] Inherit an abstract class without any constructor

查看:26
本文介绍了继承一个没有任何构造函数的抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个类继承一个类,标记为抽象的,没有定义任何构造函数.

I want to inherit a class from another class, marked as abstract, that not have any constructor defined.

这是我的代码:

// In one assembly (TheMessage.dll), as seen via F12 in VS (from Metadata)
namespace Namespace1 
{
    public abstract class Message
    {
       public string Body { get; set; }
       // some abstract methods here, not shown.
    }
}

// In another assembly (TheUser.dll)
namespace Namespace2
{
    public class MyMessage : Namespace1.Message
    {
         public MyMessage()
         {
         }
    }
}

问题是在 public MyMessage() 构造函数上我得到错误

The problem is that on the public MyMessage() constructor I get the error

'Namespace1.Message' 类型没有定义构造函数

The type 'Namespace1.Message' has no constructors defined

我在 MSDN 站点上看到(抽象(C# 参考)第一个例子)继承一个没有构造函数的抽象类是可能的.那么,有人知道我为什么会收到此错误吗?

I saw on MSDN site (abstract (C# Reference) first example) that inheriting an abstract class without constructor is possible. So, anyone know why I get this error?

请注意,当类型与 Message 类在同一个 DLL 中时,可以很好地继承,并且它的工作原理是该程序集公开了从 Namespace1.Message 派生的一些其他类型,类似于以下内容:

Note that one can inherit just fine when type is in the same DLL as the Message class and it works as that assembly exposes some other types deriving from Namespace1.Message similar to following:

// The same assembly  as Message (TheMessage.dll), as seen via F12 in VS (from Metadata)
namespace Namespace3
{
    public class Message : Namespace1.Message
    {
        public Message() {}
        public Message(string to) {}
    }
}

<小时>

我还检查了类型..."没有定义构造函数,但它没有谈到继承,而只是新建一个实例,我显然不希望直接实例化抽象类的实例.


I've also checked The type '...' has no constructors defined, but it does not speak about inheritance but rather just new-ing up an instance and I clearly have no expectations to directly instantiate an instance of an abstract class.

推荐答案

如果 Message 类的任何实例构造函数都对您不可见(通常是因为它们都是 私有的code>而你在类之外,或者它们都是privateinternal而你在程序集之外),你不能写一个继承自Message的类.(好吧,您可以使用 :this(...) 语法创建两个相互循环链接的实例构造函数,但这没有用.

If none of the instance constructors of the Message class are visible to you (typically because they are all private and you are outside the class, or they are all private or internal and you are outside the assembly), you cannot write a class that inherits from Message. (Well, you could make two instance constructors which chain each other cyclicly with the :this(...) syntax, but that would not be useful).

请注意,当您查看元数据"(为您引用的程序集生成的反射伪 C#)时,您通常只会看到可见"成员,因此任何 private内部 成员不会出现.我认为您查看元数据是因为我们看到了其主体不存在的非抽象(和非外部)方法(只是一个分号 ; 而不是主体 { ... }).

Note that when you look at the "metadata" (reflection generated pseudo-C# for an assembly you refer), you typically only see the "visible" members, so any private or internal members will not show up. I think you look at the metadata because we see non-abstract (and non-extern) methods whoses bodies are absent (just a semicolon ; there instead of a body { ... }).

Message 类的源代码将有一个或多个构造函数,每个构造函数都是 privateinternal,但是从程序集外部看时,这些都是不存在的".

The source code of your Message class will have one or more constructors, each private or internal, but when seen from outside the assembly, these are "non-existent".

如果非静态 C# 类 Message 的源代码不包含实例构造函数,编译器会自动生成一个.如果类是具体的(即非abstratc),它将是一个public 无参数构造函数,如果类是抽象的,它将是一个protected 构造函数.

If the source code of a non-static C# class Message contains no instance constructors, the compiler will generate one automatically. It will be a public parameterless constructor if the class is concrete (i.e. non-abstratc), and a protected one if the class is abstract.

这意味着如果源代码如下所示:

That means that if the source code looks like this:

public abstract class Message
{
  // note: zero non-static constructors here
}

它将完全按照它所说的进行编译:

it will be compiled exactly as if it had said:

public abstract class Message
{
  protected Message()
  {
  }
}

在这种情况下,生成的实例构造函数可供 所有 类访问,这些类派生自 Message.

and in that case the generated instance constructor is accessible to all classes deriving from Message.

这篇关于继承一个没有任何构造函数的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆