views:

284

answers:

1

I'm working in VS 2010 and working on upgrading our application to .NET 4. The application is build with Excel as the base and we want to take advantage of some of the improvements to .NET for using Excel. But I ran across a strange error that seems to be caused by using an Excel Interop object in a generic dictionary. Here is the error that was generated:

C:\MyApp\TheAssembly\MyClass.cs(823,57): 
error CS1769: Type 'MyApp\OtherAssemply.IMyController.SheetReports' from assembly 'c:\MyApp\OtherAssemply.\bin\Debug\OtherAssembly.dll' 
 cannot be used across assembly boundaries because it has a generic type 
 parameter that is an embedded interop type.

Here's the actual property that has the issue:

Dictionary<Excel.Worksheet, IReportSheet> SheetReports { get;}

Are we unable to use Interop objects in generic objects? If so, this is a serious limitation in .NET 4.0. I tried setting the Embed Interop property to false, but that didn't seem to change anything. Please let me know if there is anyway around this.

Cheers!

Erick

+1  A: 

A new feature of VS2010 is to embed the interop types into the assembly instead of using external interop assemblies.

The benefit is that you don't need to distribute the interop assemblies.

The drawback is that each assembly get its own set of the interop types.

Since the type "Excel.Worksheet" is now internal to your assembly, other assemblies can't use a generic type based on it (which is what the error message says)

You get a similar error if you do

internal class X { }
public class Y {
    public List<X> l;
}

I have not used VS2010 but I'm sure there must be an option somewhere where you can turn off the embedded interop types.

adrianm
Adrian,That's what I figured out so thanks for the confirmation. I ended up having to turn off the embedding throughout the whole solution. I'm looking into of some way around it but haven't had the time to experiment. Maybe some kind of interface? Let me know your thoughts on that.Thanks!
Erick
The only other way around it I can think of is to create some kind of assembly-affinity (like the STA threading model). You can pass the interop-objects around, by casting them as object, but as soon as you need to access them you must make the call from the assembly where they were created.On the other hand, what is wrong with using interop assemblies for office? They will be on the users machine anyway since they are part of a normal installation. You will not gain anything from embedded interop.
adrianm
correct. embedding is not that wonderful. in order to use the interops users must have a licensed copy of Office. Most will have Office 2007 which comes with the PIA's pre-installed.... :D
Anonymous Type