0

With Office interop in C#, I can write the following:

    protected override Excel.Application Create()
    {            
        return new Excel.Application();
    }

This works just fine and calls like this frequently appear in articles about Office interop, but Excel.Application is actually an interface.

Using object explorer in Visual studio, it appears that the return value of the method is a Microsoft.Office.Interop.Excel.Application, but the method being called is actually Microsoft.Office.Interop.Excel.ApplicationClass.ApplicationClass().

So how is new Application() actually getting resolved as ApplicationClass.ApplicationClass()?

Ben Aaronson
  • 6,955
  • 2
  • 23
  • 38
  • @ChrisHaas Damn, don't know how I missed that one. Thanks. – Ben Aaronson May 13 '14 at 15:00
  • This is normal behavior, as you cannot run an interface (it is just a contract). You need a concrete class to actually run behavior. And, yes, @ChrisHaas, it is essentially a duplicate. – Gregory A Beamer May 13 '14 at 15:21
  • @BenAaronson, I actually had always wondered this, too, which is why I took a little longer to dig around. – Chris Haas May 13 '14 at 15:22
  • @GregoryABeamer I'm aware of course that you can't *actually* construct an interface, what I was confused about was why you could write `new Application()` and it would know the specific implenting class's constructor to call. But the answer on the other question explains that. – Ben Aaronson May 13 '14 at 15:25
  • @BenAaronson - Never sure on here what the depths of knowledge is. Yes, there is some "magic" under the hood in the internals. ;-) – Gregory A Beamer May 13 '14 at 15:27

0 Answers0