难道我们不能使用通用对象互操作对象在.NET 4.0中? [英] Are we unable to use Interop objects in generic objects in .NET 4.0?

查看:267
本文介绍了难道我们不能使用通用对象互操作对象在.NET 4.0中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在VS 2010和工作在我们的应用程序升级到.NET 4的应用程序与Excel作为基础建设,我们要采取的一些改进功能,以.NET为使用Excel。但我跑过一个奇怪的错误,这似乎是在一个通用的字典中使用Excel互操作对象引起的。这里是生成的错误:

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;}

难道我们不能使用互操作对象一般对象?如果是这样,这是在.NET 4.0中一个严重的限制。我试图嵌入互操作属性设置为false,但这似乎并没有改变任何东西。请让我知道是否有无论如何解决这个问题。

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.

干杯!

埃里克

推荐答案

VS2010的一个新特点是嵌入使用外部互操作程序集的互操作类型到组件代替。

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.

由于类型为Excel.Worksheet现在是内部的组件,其它组件无法使用基于它泛型类型(这是什么错误消息说的)

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;
}

我没有使用VS2010,但我敢肯定,必须有一个选择的地方,你可以关闭嵌入的互操作类型。

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

这篇关于难道我们不能使用通用对象互操作对象在.NET 4.0中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆