The type '…' has no constructors defined

前端 未结 6 585
自闭症患者
自闭症患者 2020-11-27 06:07

I\'m noticing the compiler error The type \'...\' has no constructors defined generated when I erroneously attempt to instantiate a particlar class.

6条回答
  •  星月不相逢
    2020-11-27 06:19

    I've managed to reproduce this by:

    • Creating a static class in a DLL
    • Using ildasm to decompile it to IL
    • Editing the IL to remove the "abstract" and "sealed" modifiers from the class
    • Rebuilding the DLL with ilasm
    • Compiling a program which tries to create an instance of the class

    If you don't remove the abstract/sealed modifiers, the C# compiler recognizes it as a static class and gives a different error message. Of course, you could start off with a "normal" type and just remove the constructors, too.

    EDIT: I actually thought I hadn't submitted this, as I saw the "internal" constructor one first. However, I'll leave it now as my version makes the C# compiler correct - there's a difference between a type having no accessible constructors and genuinely having no constructors :)

提交回复
热议问题