本文介绍了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.

提前感谢您的回答

推荐答案

如果您正在使用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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:53