1

I want to inherit from DocX, to integrate InterOp printing of Word documents:

public class WordDoc : DocX {
    static private Microsoft.Office.Interop.Word.Application wordApp;

    static WordDoc() {
    }
}

but when I try that, I get this error:

'DocX' does not contain a constructor that takes 0 arguments

From metadata I can see that no constructors are defined at all, so I reckon the empty constructor should be generated. I also do not provide a constructor for my base class.

I do have the static constructor, but when I remove it, I still get the same error.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

2 Answers2

1

Class you are trying to inherit from has one internal constructor:

internal DocX(DocX document, XElement xml)
  : base(document, xml)
{
}

So you cannot inherit from this class. If you omit default constructor, compiler will generate this:

public WordDoc() : base() {

}

But base() constructor is not accessible (internal). If you create default constructor:

public WordDoc() {

}

Compiler will still add call to base(), because you cannot inherit from class and somehow omit calling any of this base class constructors.

So instead of inheritance - use other ways, like extension methods, proxy class and so on, depending on concrete problem you are solving.

Evk
  • 98,527
  • 8
  • 141
  • 191
1

If you look at the source code, you will see that a constructor is indeed declared:

internal DocX(DocX document, XElement xml)
        : base(document, xml)
{
}

That said, considering that it is marked internal, it would seem that this class is NOT intended to be the public API and as such, you should neither attempt to instantiate it directly or inherit from it.

Rather, all of the examples seem to be relying on the static Create method in that class:

public static DocX Create(string filename, 
    DocumentTypes documentType = DocumentTypes.Document)
{
    // Store this document in memory
    MemoryStream ms = new MemoryStream();

    // Create the docx package
    //WordprocessingDocument wdDoc = WordprocessingDocument.Create(ms, 
        DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
    Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);

    PostCreation(package, documentType);
    DocX document = DocX.Load(ms);
    document.filename = filename;
    return document;
}
David L
  • 32,885
  • 8
  • 62
  • 93