Inherit an abstract class without any constructor

前端 未结 2 1742
悲&欢浪女
悲&欢浪女 2020-12-07 04:12

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

This is my code:

// In one assembly (TheMess         


        
相关标签:
2条回答
  • 2020-12-07 04:53

    You probably have an internal constructor (not shown in the code that you posted) and are trying to instantiate the class with the internal constructor from a different assembly.

    (The default constructor for a base class is automatically called from a derived class if you don't explicitly specify a base class constructor to call, so it might not be obvious to you that the base class constructor is being called.)

    For example, if one assembly contains this class (inside namespace ClassLibrary1):

    public class Base
    {
        internal Base()
        {
        }
    }
    

    And a DIFFERENT assembly does this:

    class Derived: Base
    {
        public Derived()
        {
        }
    }
    

    You will see the following compile error:

    The type 'ClassLibrary1.Base' has no constructors defined

    0 讨论(0)
  • 2020-12-07 04:54

    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).

    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 { ... }).

    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".

    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()
      {
      }
    }
    

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

    0 讨论(0)
提交回复
热议问题