通过 Excel 2007 调用时 Asssembly.LoadFrom 失败
我有一个 .NET 4.0 类,它使用 Assembly.LoadFrom 加载 .NET 3.5 混合模式程序集。当从 .NET 4.0 应用程序调用此类时(在配置文件中指定 useLegacyV2RuntimeActivationPolicy="true"),一切正常。
但是,此类也是 COM 可见的,当我从 Excel 2007 调用它时(同样,Excel.exe.config 指定 useLegacyV2RuntimeActivationPolicy="true"),我收到 FileLoadException:
Could not load file or assembly 'Foo...'或其依赖项之一。无法加载运行时。 (来自 HRESULT 的异常:0x80131700)
内部异常是 System.Runtime.InteropServices.COMException:无法加载运行时。 (HRESULT 异常:0x80131700)
从 Excel 2003、Excel 2010 或 vbscript 调用时,我没有遇到此问题。有人可以解释发生了什么事以及我该如何解决它吗?
I have a .NET 4.0 class that uses Assembly.LoadFrom to load a .NET 3.5 mixed mode assembly. When this class is called from a .NET 4.0 app (with useLegacyV2RuntimeActivationPolicy="true" specified in the config file) it all works fine.
However, this class is also COM visible, and when I then call it from Excel 2007 (again, Excel.exe.config specifies useLegacyV2RuntimeActivationPolicy="true") I get a FileLoadException:
Could not load file or assembly 'Foo...' or one of its dependencies. Failed to load the runtime. (Exception from HRESULT: 0x80131700)
The inner exception is a System.Runtime.InteropServices.COMException: Failed to load the runtime. (Exception from HRESULT: 0x80131700)
I do not get this problem when calling from Excel 2003 or Excel 2010 or vbscript. Can someone explain what is going on and how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从 Microsoft 发现这是 Excel 2007 的一个已知问题。该问题已在 Excel 2010 中得到解决,并且该问题不会在 Excel 2007 中得到解决。
解决方法是强制 Excel 使用 COM 公开的 .NET使用 .NET 4.0 类之前先输入 2.0 类型。为此,我添加了对 .NET 2.0 版本的 mscorlib.tlb 的引用,然后在调用 .NET 4.0 类之前添加以下 VBA 代码:
希望这对任何偶然发现这个晦涩问题的人来说都是有用的!
I've discovered from Microsoft that this is a known issue with Excel 2007. The problem has been resolved in Excel 2010, and the issue will not be fixed in Excel 2007.
The workaround is to force Excel to use a COM-exposed .NET 2.0 type before using the .NET 4.0 class. I did this by adding a reference to the .NET 2.0 version of mscorlib.tlb, and then adding the following VBA code before calling the .NET 4.0 class:
Hope this proves useful to anyone who stumbles across this obscure issue!