System.InvalidCastException:无法将System .__ ComObject类型的对象强制转换为Excel; [英] System.InvalidCastException : Unable to cast object of type System.__ComObject to type Excel;

查看:532
本文介绍了System.InvalidCastException:无法将System .__ ComObject类型的对象强制转换为Excel;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我的代码非常简单。

            var excelApp = new Excel.Application();
            Excel.Workbook wb = excelApp.Workbooks.Open("Path");
            Excel.Worksheet ws = wb.Worksheets["HeaderName"];
            excelApp.Visible = true;
            excelApp.UserControl = false;
            ws.get_Range("A1").Value2 = "John";
            wb.Close(true);

此代码在单元测试面板中连续运行多次,偶尔
此异常将被抛出:

This code is run several times successively in a panel of unit test, and occasionally this Exception will be thrown :

------------------------------- --------------------------

---------------------------------------------------------

结果讯息:      System.InvalidCastException:无法将类型为'System .__ ComObject'的对象强制转换为'Microsoft.Office.Interop.Excel.ApplicationClass'。

Result Message:    System.InvalidCastException : Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.Excel.ApplicationClass'.

结果StackTrace:     
$
在System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)

  在System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)

  在System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType,Object [] props,Boolean bNewObj)

  在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck)

  在System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark)

  在System.Activator.CreateInstance(Type type,Boolean nonPublic)

  在System.Activator.CreateInstance(类型类型)

  在Program.MyProgramTest.MethodTest()

---------------------------------- ---------------------------------------



这是一个非常随机的错误:如果我重新运行引发异常的MethodTest(),它将成功运行。

Result StackTrace:    
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Program.MyProgramTest.MethodTest()
-------------------------------------------------------------------------

It's a really random bug: if I rerun the MethodTest() that threw the exception, it will run with success.

看起来这个随机异常发生是已知的错误 但它似乎还没有修复。

It seems like this random exception occurring is a known bug  but it doesn't seem to be fixed yet.

提前感谢您的回答

推荐答案

这听起来像单元测试框架的问题。您正在谈论调用需要STA的COM对象。如果您的测试框架没有在STA中运行测试,那么您将遇到问题。如果您已将测试配置为并行运行
,那么您也可能遇到尝试跨线程访问各种COM对象的问题,因为它们是STA。由测试框架来管理所有这些。

This really sounds like an issue with the unit test framework. You're talking about calling a COM object which requires an STA. If your test framework doesn't run tests in an STA then you'll have problems. If you have configured your tests to run in parallel then you could also run into issues trying to access the various COM objects across threads, because they are STAs. It is up to the test framework to manage all this.

如果您正在使用MSTest,那么我们可以将您的问题转移到他们的测试论坛。如果您使用的是NUnit或其他测试框架,那么您应该在论坛中发布您的问题。

If you are using MSTest then we can move your question to their test forum. If you are using NUnit or another test framework then you should post your question in their forums.


这篇关于System.InvalidCastException:无法将System .__ ComObject类型的对象强制转换为Excel;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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