(Note: I claim this is not a duplicate of Clean up Excel Interop Objects with IDisposable. That question asks "How do I do it?" My primary question is "Why am I getting C# compiler errors?")
Primary goal here is to read (and later write) Excel files from a C# program. Google suggests that I should add a reference to Microsoft.Office.Interop.Excel and run from there. I saw enough code to know that I'm doing COM interop, and that I therefore want to dispose the COM objects when I'm done with them. With the reference added (to version 14.0.0.0 of Microsoft.Office.Interop.Excel, in Visual Studio 2012), I'm off to the races.
Now, I don't trust myself remember to get all the calls to Marshal.ReleaseComObject
in all the right places, so I want to put my COM objects into using
statements. Microsoft.Office.Interop.Excel.Application
is not IDisposable
, and, as far as I've discovered, neither is anything else in the Microsoft.Office.Interop.Excel
namespace. But that's fine. I'm smart. I know how to fix this:
using Excel = Microsoft.Office.Interop.Excel;
//...
sealed class ExcelWrapper<T> : IDisposable
{
public T Child { get; private set; }
public ExcelWrapper(T child) { Child = child; }
public static implicit operator ExcelWrapper<T>(T child) { return new ExcelWrapper<T>(child); }
public static implicit operator T(ExcelWrapper<T> host) { return host.Child; }
public void Dispose()
{
try { Marshal.ReleaseComObject(Child); }
catch { }
}
}
//...
using (ExcelWrapper<Excel.Application> _xlApp = (ExcelWrapper<Excel.Application>)new Excel.Application()) // CS0030
{
Excel.Application xlApp = _xlApp; // CS0029 and CS0266
}
This presents me with error messages I don't understand (comments above indicate the lines with errors):
error CS0030: Cannot convert type 'Microsoft.Office.Interop.Excel.Application' to 'ExcelWrapper<Microsoft.Office.Interop.Excel.Application>'
error CS0029: Cannot implicitly convert type 'ExcelWrapper<Microsoft.Office.Interop.Excel.Application>' to 'Microsoft.Office.Interop.Excel.Application'
Why am I getting these error messages? Did I not type public static implicit operator
? For some reason, I tried unsealing ExcelWrapper
.Things changed, but I still don't understand what's going on:
error CS0266: Cannot implicitly convert type 'ExcelWrapper<Microsoft.Office.Interop.Excel.Application>' to 'Microsoft.Office.Interop.Excel.Application'. An explicit conversion exists (are you missing a cast?)
I'm still pretty sure an implicit conversion exists. But I can placate the compiler by using an explicit conversion, as long as I'm OK with leaving ExcelWrapper
unsealed.
So, what am I doing wrong here?
Why does the compiler appear to completely ignore my conversion operators when the ExcelWrapper
class is sealed? And why does the compiler appear to ignore the implicit
designation (but not the rest of the operator) when the ExcelWrapper
class is not sealed?