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 UserRank #1,422 · 1.2K reputationPosted Apr 4, 2011, 5:25 AM EDT
http://msdn.microsoft.com/en-us/library/ms247302%28v=office.11%29.aspx
VulpesRank #55 · 29.2K reputationPosted Apr 4, 2011, 6:36 AM EDT
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.
Guest UserRank #1,422 · 1.2K reputationPosted Apr 4, 2011, 6:58 AM EDT
Thanks Vulpes!
David Rougier8 reputationPosted Apr 4, 2011, 12:27 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 4, 2011, 2:41 PM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Apr 4, 2011, 3:15 PM EDT
I also used it in my sample program for decompiling with .NET Reflector.
VulpesRank #55 · 29.2K reputationPosted Apr 4, 2011, 3:36 PM EDT
It would, of course, work if he used fully qualified names throughout.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 4, 2011, 6:52 PM EDT
I deleted my message that was my second reply.
VulpesRank #55 · 29.2K reputationPosted Apr 5, 2011, 2:20 AM EDT