Hi everyone,
I am going to manipulate an Excel workbook via C# programming. I looked to some information to do so. A C# statement disturbed me while reading. Here is the statement :
Excel.Application oXl = new Excel.Application;
"Excel" is an alias for a namespace. The statement disturbed me because "Application" is an interface and it does not contain any code by definition. The fact that the object library allows the programmer to "control" Excel may justify the instanciation. Could someone explain me clearly why the instanciation is possible ?
Thanks in advance.
Loading
Guest UserPosted Apr 4, 2011, 9:25 AM
http://msdn.microsoft.com/en-us/library/ms247302%28v=office.11%29.aspx
VulpesPosted Apr 5, 2011, 6:20 AM
Sam HobbsPosted Apr 4, 2011, 10:52 PM
I deleted my message that was my second reply.
VulpesPosted Apr 4, 2011, 7:36 PM
It would, of course, work if he used fully qualified names throughout.
VulpesPosted Apr 4, 2011, 7:15 PM
I also used it in my sample program for decompiling with .NET Reflector.
Sam HobbsPosted Apr 4, 2011, 6:41 PM
using Excel = Microsoft.Office.Interop.Excel;
Somewhere in the soruce code? If there is such a line, try commenting it out to see what happens.
David RougierPosted Apr 4, 2011, 4:27 PM
Guest UserPosted Apr 4, 2011, 10:58 AM
Thanks Vulpes!
VulpesPosted Apr 4, 2011, 10:36 AM
The only place I've seen this previously was in an obscure book I have on Office interop.
The Application class is well and truly hidden and the Object Browser is unable to show it, probably because it can't cope with both an interface and a class, which are within the same namespace, having the same name! However, if you look at this line:
Excel.Application oXl = new Excel.Application();
with Redgate Reflector, then it decompiles back to this line:
Application application1 = (Application) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
proving that the Application object is created behind the scenes using its CLSID and then cast back to the Application interface. So evidently the C# compiler is somehow able to sort out what's needed.